POJ_2632_Crashing Robots(模拟法)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # include <stdio.h> 2 # include <string.h> 3 struct node 4 { 5 int x; 6 int y; 7 char c; 8 }q[111]; 9 struct node p[111]; 10 int main() 11 { 12 // freopen("d:\\my.txt", "r", stdin); 13 // freopen("d:\\data.txt", "w", stdout); 14 char ch[101][101],leag; 15 char str[]={'N','W','S','E'}; 16 int i,j,k,t,nn,n,m,a,b; 17 int t1,t2,ii,jj; 18 char c3; 19 scanf("%d",&nn); 20 while(nn--) 21 { 22 leag=1; 23 memset(ch,'0',sizeof(ch)); 24 scanf("%d%d",&m,&n); 25 scanf("%d%d",&a,&b); 26 for(i=0;i<a;i++) 27 { 28 scanf("%d%d %c",&p[i].x,&p[i].y,&p[i].c); 29 ch[p[i].x-1][p[i].y-1]='1'; 30 } 31 for(i=0;i<b;i++) 32 { 33 scanf("%d %c%d",&q[i].x,&q[i].c,&q[i].y); 34 } 35 for(i=0;i<b;i++) 36 { 37 t1=q[i].x; 38 c3=q[i].c; 39 t2=q[i].y; 40 if(c3 == 'F') 41 { 42 ii=p[t1-1].x-1; 43 jj=p[t1-1].y-1; 44 ch[ii][jj]='0'; 45 if(p[t1-1].c=='W') 46 { 47 while(t2--) 48 { 49 50 if(ii==0) 51 { 52 printf("Robot %d crashes into the wall\n",t1); 53 leag=0; 54 break; 55 }ii--; 56 if(ch[ii][jj]=='1') 57 { 58 for(k=0;k<a;k++) 59 { 60 if(p[k].x==ii+1 && p[k].y==jj+1) 61 break; 62 } 63 printf("Robot %d crashes into robot %d\n",t1,k+1); 64 leag=0; 65 break; 66 } 67 } 68 ch[ii][jj]='1'; 69 p[t1-1].x=ii+1; 70 p[t1-1].y=jj+1; 71 } 72 else if(p[t1-1].c=='E') 73 { 74 while(t2--) 75 { 76 77 if(ii == m) 78 { 79 printf("Robot %d crashes into the wall\n",t1); 80 leag=0; 81 break; 82 }ii++; 83 if(ch[ii][jj]=='1') 84 { 85 for(k=0;k<a;k++) 86 { 87 if(p[k].x==ii+1 && p[k].y==jj+1) 88 break; 89 } 90 printf("Robot %d crashes into robot %d\n",t1,k+1); 91 leag=0; 92 break; 93 } 94 } 95 ch[ii][jj]='1'; 96 p[t1-1].x=ii+1; 97 p[t1-1].y=jj+1; 98 } 99 else if(p[t1-1].c=='N') 100 { 101 while(t2--) 102 { 103 104 if(jj==n) 105 { 106 printf("Robot %d crashes into the wall\n",t1); 107 leag=0; 108 break; 109 }jj++; 110 if(ch[ii][jj]=='1') 111 { 112 for(k=0;k<a;k++) 113 { 114 if(p[k].x==ii+1 && p[k].y==jj+1) 115 break; 116 } 117 printf("Robot %d crashes into robot %d\n",t1,k+1); 118 leag=0; 119 break; 120 } 121 } 122 ch[ii][jj]='1'; 123 p[t1-1].x=ii+1; 124 p[t1-1].y=jj+1; 125 } 126 else if (p[t1-1].c=='S') 127 { 128 while(t2--) 129 { 130 131 if(jj==0) 132 { 133 printf("Robot %d crashes into the wall\n",t1); 134 leag=0; 135 break; 136 } 137 jj--; 138 if(ch[ii][jj]=='1') 139 { 140 for(k=0;k<a;k++) 141 { 142 if(p[k].x==ii+1 && p[k].y==jj+1) 143 break; 144 } 145 printf("Robot %d crashes into robot %d\n",t1,k+1); 146 leag=0; 147 break; 148 } 149 } 150 ch[ii][jj]='1'; 151 p[t1-1].x=ii+1; 152 p[t1-1].y=jj+1; 153 } 154 155 } 156 else if(c3 == 'L') 157 { 158 int temp; 159 temp=t2%4; 160 if(temp!=0) 161 { 162 for(k=0;k<4;k++) 163 { 164 if(p[t1-1].c == str[k]) 165 break; 166 } 167 p[t1-1].c=str[(k+temp)%4]; 168 } 169 } 170 else if(c3 == 'R') 171 { 172 int temp; 173 temp=t2%4; 174 if(temp!=0) 175 { 176 for(k=0;k<4;k++) 177 { 178 if(p[t1-1].c == str[k]) 179 break; 180 } 181 p[t1-1].c=str[(k+4-temp)%4]; 182 } 183 } 184 if(leag==0) 185 break; 186 } 187 if(leag) 188 printf("OK\n"); 189 } 190 return 0; 191 }