做模拟题做的我直接睡着了,题并不难,就是一个细心的问题,有一些细节问题注意了就差不多了,代码写的精美的一般找错误也好找一些,应该学着些好看的代码
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std ; 5 int len,high; 6 int a,b,j ; 7 struct node 8 { 9 int x; 10 int y ; 11 int dire; 12 } s[10001]; 13 int judge(int k) 14 { 15 int i; 16 if(s[k].x > len||s[k].x< 1||s[k].y>high||s[k].y<1) 17 { 18 printf("Robot %d crashes into the wall ",k); 19 return 0; 20 } 21 for(i = 1 ; i <= a ; i++) 22 { 23 if(i == k) 24 continue; 25 if(s[i].x == s[k].x&&s[i].y == s[k].y) 26 { 27 printf("Robot %d crashes into robot %d ",k,i); 28 return 0 ; 29 } 30 } 31 return 1 ; 32 } 33 int main() 34 { 35 int n ; 36 cin>>n; 37 for(int i = 1 ; i <= n ; i++) 38 { 39 cin>>len>>high; 40 cin>>a>>b ; 41 char dire ; 42 for(j = 1 ; j <= a ; j++) 43 { 44 cin>>s[j].x>>s[j].y>>dire; 45 if(dire == 'N') 46 s[j].dire = 0 ; 47 if(dire == 'W') 48 s[j].dire = 1 ; 49 if(dire == 'S') 50 s[j].dire = 2 ; 51 if(dire == 'E') 52 s[j].dire = 3 ; 53 } 54 int num,repeat,flag = 1 ; 55 char order ; 56 for(j = 1 ; j <= b ; j++) 57 { 58 cin>>num>>order>>repeat ; 59 for(int h = 1 ; h <= repeat ; h++ )//把这个放在外边是为了底下的左右指令时比较好处理 60 { 61 if(order == 'F') 62 { 63 if(s[num].dire == 3) 64 { 65 s[num].x++ ; 66 if(!judge(num)) 67 { 68 flag = 0 ; 69 break ; 70 } 71 } 72 if(s[num].dire == 1) 73 { 74 s[num].x--; 75 if(!judge(num)) 76 { 77 flag = 0 ; 78 break ; 79 } 80 } 81 if(s[num].dire == 0) 82 { 83 s[num].y++ ; 84 if(!judge(num)) 85 { 86 flag = 0 ; 87 break ; 88 } 89 } 90 if(s[num].dire == 2) 91 { 92 s[num].y--; 93 if(!judge(num)) 94 { 95 flag = 0 ; 96 break ; 97 } 98 } 99 } 100 101 if(order == 'L') 102 s[num].dire = (1+s[num].dire)%4 ; 103 if(order == 'R') 104 s[num].dire = (s[num].dire-1+4)%4; 105 } 106 if(flag == 0) 107 break ; 108 } 109 if(j < b) 110 for(++j ; j <= b ; j++) 111 { 112 cin>>num>>order>>repeat ; 113 } 114 if(flag == 1) 115 printf("OK "); 116 } 117 return 0 ; 118 }