Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
搞了整整一晚上,代码有些烂#include<cstdio> #include<iostream> #include<string> #include<algorithm> #include<vector> #include<numeric> #include<cmath> #include<bitset> #include<set> #include<queue> #include<iterator> #include<limits> #include<map> #include<cstring> using namespace std; #define EAST 0 #define WEST 1 #define NORTH 2 #define SOUTH 3; typedef struct robot { int x; int y; char direction; }; int main(int argc, char* argv[]) { int K,A,B,N,M,i,j,k; int t1,t2,num,step,tmp,r1,r2; char ch; bool rr,rw,ok,flag; int arr[110][110]; map<int,robot> m; map<int,robot>::iterator it,tranverse; scanf("%d",&K); while(K > 0) { rr = rw = ok = false; flag = false; scanf("%d %d",&A,&B); scanf("%d %d",&N,&M); num = 0; memset(arr,0,sizeof(arr)); for(i = 0; i < N; ++i) { scanf("%d %d %c",&t1,&t2,&ch); robot r; r.x = B - t2 + 1; r.y = t1;//坐标原点由左下角换成左上角,也就是反转纵坐标 r.direction = ch; ++num; m.insert(make_pair(num,r)); arr[B - t2 + 1][t1] = 1;//坐标原点由左下角换成左上角,也就是反转纵坐标 } for(i = 0; i < M; ++i) { scanf("%d %c %d",&num,&ch,&step); it = m.find(num); if(ch == 'F' && flag == false) { if(it->second.direction == 'E') { //要移动的下一步有机器人存在 tmp = it->second.y + step; for(k = it->second.x,j = it->second.y + 1; j <= tmp; ++j) { if(j > A) break; if(arr[k][j] == 1 && flag == false) { rr = true; r1 = num; for(tranverse = m.begin(); tranverse != m.end(); ++tranverse) { if( (tranverse->second.x == k) && (tranverse->second.y == j)) r2 = tranverse->first; } flag = true; break; } } if(flag == true) continue; //越界 if( (it->second.y + step) > A && flag == false) { rw = true; flag = true; r1 = num; continue; } if(rr == false) { arr[it->second.x][it->second.y] = 0; arr[it->second.x][it->second.y + step] = 1; it->second.y += step; } continue; } if(it->second.direction == 'W' && flag == false) { //要移动的下一步有机器人存在 tmp = it->second.y - step; for(k = it->second.x,j = it->second.y - 1; j >= tmp; --j) { if(j < 1) break; if(arr[k][j] == 1 && flag == false) { rr = true; r1 = num; for(tranverse = m.begin(); tranverse != m.end(); ++tranverse) { if( (tranverse->second.x == k) && (tranverse->second.y == j)) r2 = tranverse->first; } flag = true; break; } } if(flag == true) continue; //越界 if( (it->second.y - step) < 1 ) { rw = true; flag = true; r1 = num; continue; } if(rr == false) { arr[it->second.x][it->second.y] = 0; arr[it->second.x][it->second.y - step] = 1; it->second.y -= step; } continue; } if(it->second.direction == 'S' && flag == false) { //要移动的下一步有机器人存在 tmp = it->second.x + step; for(k = it->second.x + 1,j = it->second.y; k <= tmp; ++k) { if(k > B) break; if(arr[k][j] == 1 && flag == false) { rr = true; r1 = num; for(tranverse = m.begin(); tranverse != m.end(); ++tranverse) { if( (tranverse->second.x == k) && (tranverse->second.y == j)) r2 = tranverse->first; } flag = true; break; } } if(flag == true) continue; //越界 if( (it->second.x + step) > B ) { rw = true; r1 = num; flag = true; continue; } if(rr == false) { arr[it->second.x][it->second.y] = 0; arr[it->second.x + step][it->second.y] = 1; it->second.x += step; } continue; } if(it->second.direction == 'N' && flag == false) { //要移动的下一步有机器人存在 tmp = it->second.x - step; for(k = it->second.x - 1,j = it->second.y; k >= tmp; --k) { if(k < 1) break; if(arr[k][j] == 1 && flag == false) { rr = true; r1 = num; for(tranverse = m.begin(); tranverse != m.end(); ++tranverse) { if( (tranverse->second.x == k) && (tranverse->second.y == j)) r2 = tranverse->first; } flag = true; break; } } if(flag == true) continue; //越界 if( (it->second.x - step) < 1 ) { rw = true; r1 = num; flag = true; continue; } if(rr == false) { arr[it->second.x][it->second.y] = 0; arr[it->second.x - step][it->second.y] = 1; it->second.x -= step; } continue; } } if(ch == 'R') { tmp = step % 4; if(it->second.direction == 'E') { if(tmp == 1) it->second.direction = 'S'; if(tmp == 2) it->second.direction = 'W'; if(tmp == 3) it->second.direction = 'N'; continue; } if(it->second.direction == 'S') { if(tmp == 1) it->second.direction = 'W'; if(tmp == 2) it->second.direction = 'N'; if(tmp == 3) it->second.direction = 'E'; continue; } if(it->second.direction == 'W') { if(tmp == 1) it->second.direction = 'N'; if(tmp == 2) it->second.direction = 'E'; if(tmp == 3) it->second.direction = 'S'; continue; } if(it->second.direction == 'N') { if(tmp == 1) it->second.direction = 'E'; if(tmp == 2) it->second.direction = 'S'; if(tmp == 3) it->second.direction = 'W'; continue; } } if(ch == 'L') { tmp = step % 4; if(it->second.direction == 'E') { if(tmp == 1) it->second.direction = 'N'; if(tmp == 2) it->second.direction = 'W'; if(tmp == 3) it->second.direction = 'S'; continue; } if(it->second.direction == 'S') { if(tmp == 1) it->second.direction = 'E'; if(tmp == 2) it->second.direction = 'N'; if(tmp == 3) it->second.direction = 'W'; continue; } if(it->second.direction == 'W') { if(tmp == 1) it->second.direction = 'S'; if(tmp == 2) it->second.direction = 'E'; if(tmp == 3) it->second.direction = 'N'; continue; } if(it->second.direction == 'N') { if(tmp == 1) it->second.direction = 'W'; if(tmp == 2) it->second.direction = 'S'; if(tmp == 3) it->second.direction = 'E'; continue; } }//end if }//end for if(rw == true) printf("Robot %d crashes into the wall\n",r1); else if(rr == true) printf("Robot %d crashes into robot %d\n",r1,r2); else printf("OK\n"); m.clear(); memset(arr,0,sizeof(arr)); --K; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator