poj 1068 Parencodings
http://poj.org/problem?id=1068
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 int vis[100]; 5 int q[100]; 6 void chack(char str[]) 7 { 8 9 int count=0; 10 int i,j,k,d; 11 int len=strlen(str); 12 for(i=0;i<len;i++) 13 { 14 if(str[i]==')'&&!vis[i]) 15 { 16 vis[i]=1; 17 for(j=i-1;j>=0;j--) 18 { 19 if(str[j]=='('&&!vis[j]) 20 { 21 vis[j]=1; 22 break; 23 } 24 } 25 d=0; 26 for(k=j;k<i;k++) 27 { 28 if(str[k]=='(') 29 d++; 30 } 31 q[count++]=d; 32 } 33 } 34 for(i=0;i<count-1;i++) 35 printf("%d ",q[i]); 36 printf("%d\n",q[i]); 37 } 38 int main() 39 { 40 int i,j; 41 int s[100]; 42 int n,m; 43 char str[100]; 44 scanf("%d",&n); 45 while(n--) 46 { 47 int cot=0; 48 int k=0; 49 scanf("%d",&m); 50 for(i=0; i<m; i++) 51 scanf("%d",&s[i]); 52 for(i=0; i<m; i++) 53 { 54 for(j=cot; j<s[i]; j++) 55 str[k++]='('; 56 str[k++]=')'; 57 cot=s[i]; 58 } 59 str[k]='\0'; 60 memset(vis,0,sizeof(vis)); 61 chack(str); 62 } 63 return 0; 64 }
poj 2632 Crashing Robots
http://poj.org/problem?id=2632
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 struct node 5 { 6 int x; 7 int y; 8 char f; 9 } ro[101]; 10 struct no 11 { 12 int num; 13 char zh; 14 int count; 15 } z[101]; 16 int n,m; 17 int a,b; 18 int i,j,k; 19 int flag; 20 void zx() 21 { 22 int nu,cot; 23 char h; 24 for(i=0; i<b; i++) 25 { 26 nu=z[i].num; 27 h=z[i].zh; 28 cot=z[i].count; 29 for(j=0; j<cot; j++) 30 { 31 if(h=='F') //直走 32 { 33 if(ro[nu].f=='E') 34 ro[nu].x++; 35 else if(ro[nu].f=='W') 36 ro[nu].x--; 37 else if(ro[nu].f=='N') 38 ro[nu].y++; 39 else if(ro[nu].f=='S') 40 ro[nu].y--; 41 for(k=1; k<=a; k++) 42 { 43 if(k==nu) 44 continue; 45 if(ro[nu].x==ro[k].x&&ro[nu].y==ro[k].y) 46 { 47 flag=1; 48 printf("Robot %d crashes into robot %d\n",nu,k); 49 return; 50 } 51 } 52 if(ro[nu].x>n||ro[nu].x<1||ro[nu].y>m||ro[nu].y<1) 53 { 54 flag=1; 55 printf("Robot %d crashes into the wall\n",nu); 56 return; 57 } 58 } 59 else if(h=='L')// 左转 60 { 61 if(ro[nu].f=='E') 62 ro[nu].f='N'; 63 else if(ro[nu].f=='W') 64 ro[nu].f='S'; 65 else if(ro[nu].f=='N') 66 ro[nu].f='W'; 67 else if(ro[nu].f=='S') 68 ro[nu].f='E'; 69 } 70 else if(h=='R')//右转 71 { 72 if(ro[nu].f=='E') 73 ro[nu].f='S'; 74 else if(ro[nu].f=='W') 75 ro[nu].f='N'; 76 else if(ro[nu].f=='N') 77 ro[nu].f='E'; 78 else if(ro[nu].f=='S') 79 ro[nu].f='W'; 80 } 81 } 82 } 83 if(flag==0) 84 printf("OK\n"); 85 } 86 int main() 87 { 88 89 int t; 90 scanf("%d",&t); 91 while(t--) 92 { 93 scanf("%d %d",&n,&m); 94 scanf("%d %d",&a,&b); 95 flag=0; 96 for(i=1; i<=a; i++)//输入机器人信息 97 scanf("%d %d %c",&ro[i].x,&ro[i].y,&ro[i].f); 98 for(i=0; i<b; i++)?//输入命令 99 scanf("%d %c %d",&z[i].num,&z[i].zh,&z[i].count); 100 zx();//执行命令 101 } 102 return 0; 103 104 }
poj 1573 Robot Motion
http://poj.org/problem?id=1573
1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 int row,col,entry; 6 char s[12][12]; 7 while(~scanf("%d %d %d%*c",&row,&col,&entry)) 8 { 9 if(!(row ||col || entry)) 10 break; 11 memset(s,'O',sizeof(s)); 12 int i,j; 13 char ch; 14 for(i=1;i<=row;i++) 15 { 16 for(j=1;j<=col;j++) 17 scanf("%c",&s[i][j]); 18 scanf("%*c",&ch); 19 } 20 int flag[12][12]={0}; 21 int count; 22 int r=1; 23 int c=entry; 24 for(count=0; ;count++) 25 { 26 flag[r][c]++; 27 if(s[r][c]=='N') 28 r--; 29 else if(s[r][c]=='S') 30 r++; 31 else if(s[r][c]=='W') 32 c--; 33 else if(s[r][c]=='E') 34 c++; 35 else if(s[r][c]=='O') 36 { 37 printf("%d step(s) to exit\n",count); 38 break; 39 } 40 if(flag[r][c]==2) 41 { 42 row=r; 43 col=c; 44 int flg=1; 45 for(r=1,c=entry,count=0;;count++) 46 { 47 if(r==row && c==col && flg==1) 48 { 49 printf("%d step(s) before a loop of ",count); 50 count=0; 51 flg++; 52 } 53 if(r==row && c==col && count!=0 && flg==2) 54 { 55 printf("%d step(s)\n",count); 56 break; 57 } 58 if(s[r][c]=='N') 59 r--; 60 else if(s[r][c]=='S') 61 r++; 62 else if(s[r][c]=='W') 63 c--; 64 else if(s[r][c]=='E') 65 c++; 66 } 67 break; 68 } 69 } 70 } 71 return 0; 72 }
poj 2993 Emag eht htiw Em Pleh
http://poj.org/problem?id=2993
1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 int i,j,k; 6 char s[17][33]; 7 memset(s,'.',sizeof(s));//棋盘初始化 8 for(k=0; k<17; k+=2) 9 for(j=0; j<33; j+=4) 10 s[k][j]='+'; 11 for(k=1; k<17; k+=2) 12 for(j=0; j<33; j+=4) 13 s[k][j]='|'; 14 for(k=0; k<17; k+=2) 15 for(j=1; j<33; j+=4) 16 s[k][j]='-'; 17 for(k=0; k<17; k+=2) 18 for(j=2; j<33; j+=4) 19 s[k][j]='-'; 20 for(k=0; k<17; k+=2) 21 for(j=3; j<33; j+=4) 22 s[k][j]='-'; 23 for(k=1; k<17; k+=4) 24 for(j=6; j<33; j+=8) 25 s[k][j]=':'; 26 for(k=1; k<17; k+=4) 27 for(j=5; j<33; j+=8) 28 s[k][j]=':'; 29 for(k=1; k<17; k+=4) 30 for(j=7; j<33; j+=8) 31 s[k][j]=':'; 32 for(k=3; k<17; k+=4) 33 for(j=2; j<33; j+=8) 34 s[k][j]=':'; 35 for(k=3; k<17; k+=4) 36 for(j=1; j<33; j+=8) 37 s[k][j]=':'; 38 for(k=3; k<17; k+=4) 39 for(j=3; j<33; j+=8) 40 s[k][j]=':'; 41 char str1[20]; 42 char str2[100]; 43 scanf("%s %s",str1,str2);//输入棋子信息 44 if(strcmp(str1,"White:") == 0) 45 { 46 for(i = 0; str2[i]; i++) 47 { 48 if(str2[i] == ',') 49 continue; 50 else if(str2[i]>='A' && str2[i] <= 'Z') 51 { 52 int y = str2[i+1]-'a'; 53 int x = str2[i+2]-'0'; 54 int row = (8-x)*2 + 1; 55 int col = y*4 + 2; 56 s[row][col] = str2[i]; 57 i += 2; 58 } 59 else if(str2[i] >= 'a' && str2[i] <= 'z' && str2[i-1] == ',') 60 { 61 int y = str2[i]-'a'; 62 int x = str2[i+1]-'0'; 63 int row = (8-x)*2 + 1; 64 int col = y*4 + 2; 65 s[row][col] = 'P'; 66 i += 1; 67 } 68 } 69 } 70 scanf("%s %s",str1,str2); 71 if(strcmp(str1,"Black:") == 0) 72 { 73 for(i = 0; str2[i]; i++) 74 { 75 if(str2[i] == ',') 76 continue; 77 else if(str2[i]>='A' && str2[i] <= 'Z') 78 { 79 int y = str2[i+1]-'a'; 80 int x = str2[i+2]-'0'; 81 int row = (8-x)*2 + 1; 82 int col = y*4 + 2; 83 s[row][col] = str2[i]+32; 84 i += 2; 85 } 86 else if(str2[i] >= 'a' && str2[i] <= 'z' && str2[i-1] == ',') 87 { 88 int y = str2[i]-'a'; 89 int x = str2[i+1]-'0'; 90 int row = (8-x)*2 + 1; 91 int col = y*4 + 2; 92 s[row][col] = 'p'; 93 i += 1; 94 } 95 } 96 } 97 for(i=0; i<17; i++) 98 { 99 for(j=0; j<33; j++) 100 printf("%c",s[i][j]); 101 printf("\n"); 102 } 103 return 0; 104 }
poj 2996 Help Me with the Game
http://poj.org/problem?id=2996
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 struct node 5 {char f,h,l;}; 6 int main() 7 { 8 int i,j; 9 node w[100]; 10 node b[100]; 11 node p[100]; 12 char s[50][100]; 13 for(i=0; i<17; i++) 14 scanf("%s%*c",s[i]); 15 int count=0,cot=0; 16 for(i=0; i<17; i++) 17 { 18 for(j=0; j<33; j++) 19 { 20 if(s[i][j]>='A'&&s[i][j]<='Z') 21 { 22 w[count].f=s[i][j]; 23 if(i==16) 24 w[count].h='1'; 25 else 26 w[count].h='1'+(16-i)/2; 27 if(j==2) 28 w[count].l='a'; 29 else 30 w[count].l='a'+(j-2)/4; 31 count++; 32 } 33 else if(s[i][j]>='a'&&s[i][j]<='z') 34 { 35 b[cot].f=s[i][j]; 36 if(i==16) 37 b[cot].h='1'; 38 else 39 b[cot].h='1'+(16-i)/2; 40 if(j==2) 41 b[cot].l='a'; 42 else 43 b[cot].l='a'+(j-2)/4; 44 cot++; 45 } 46 } 47 } 48 node k; 49 for( i=0; i<count-1 ; i++ ) 50 for(j=i+1; j<count; j++) 51 { 52 if(w[i].f>w[j].f) 53 { 54 k=w[i]; 55 w[i]=w[j]; 56 w[j]=k; 57 } 58 else if(w[i].f==w[j].f) 59 { 60 if(w[i].h>w[j].h) 61 { 62 k=w[i]; 63 w[i]=w[j]; 64 w[j]=k; 65 } 66 else if(w[i].h==w[j].h) 67 { 68 if(w[i].l>w[j].l) 69 { 70 k=w[i]; 71 w[i]=w[j]; 72 w[j]=k; 73 } 74 } 75 } 76 } 77 printf("White: "); 78 for(i=0; i<count; i++) 79 if(w[i].f=='K') 80 { 81 printf("%c%c%c,",w[i].f,w[i].l,w[i].h); 82 break; 83 } 84 for(i=0; i<count; i++) 85 if(w[i].f=='Q') 86 { 87 printf("%c%c%c,",w[i].f,w[i].l,w[i].h); 88 break; 89 } 90 for(i=0; i<count; i++) 91 if(w[i].f=='R') 92 printf("%c%c%c,",w[i].f,w[i].l,w[i].h); 93 for(i=0; i<count; i++) 94 if(w[i].f=='B') 95 printf("%c%c%c,",w[i].f,w[i].l,w[i].h); 96 for(i=0; i<count; i++) 97 if(w[i].f=='N') 98 printf("%c%c%c,",w[i].f,w[i].l,w[i].h); 99 int a; 100 a=0; 101 for(i=0; i<count; i++) 102 { 103 if(w[i].f=='P') 104 p[a++]=w[i]; 105 } 106 for(i=0;i<a;i++) 107 { 108 if(i==a-1) 109 printf("%c%c\n",p[i].l,p[i].h); 110 else printf("%c%c,",p[i].l,p[i].h); 111 } 112 printf("Black: "); 113 for(i=0; i<cot; i++) 114 if(b[i].f=='k') 115 { 116 printf("K%c%c,",b[i].l,b[i].h); 117 break; 118 } 119 for(i=0; i<cot; i++) 120 if(b[i].f=='q') 121 { 122 printf("Q%c%c,",b[i].l,b[i].h); 123 break; 124 } 125 for(i=0; i<cot; i++) 126 if(b[i].f=='r') 127 printf("R%c%c,",b[i].l,b[i].h); 128 for(i=0; i<cot; i++) 129 if(b[i].f=='b') 130 printf("B%c%c,",b[i].l,b[i].h); 131 for(i=0; i<cot; i++) 132 if(b[i].f=='n') 133 printf("N%c%c,",b[i].l,b[i].h); 134 a=0; 135 for(i=0; i<cot; i++) 136 { 137 if(b[i].f=='p') 138 p[a++]=b[i]; 139 } 140 for(i=0;i<a;i++) 141 { 142 if(i==a-1) 143 printf("%c%c\n",p[i].l,p[i].h); 144 else printf("%c%c,",p[i].l,p[i].h); 145 } 146 return 0; 147 }
模拟题不需要算法,只需严格按照题意将代码敲出来即可,过程一般比较麻烦,需要考虑的情况比较多。