poj 2632 Crashing Robots_模拟

来源:互联网 发布:淘宝旗舰店会有假货吗 编辑:程序博客网 时间:2024/06/04 10:21

做的差点想吐,调来调去,编译器都犯毛病,wlgq,幸好1a。

题意:给你机器人怎走的路线,碰撞就输出

#include <cstdlib>#include <iostream>#include<cstdio>#include<cstring>#define N 110using namespace std;struct Rob{int x,y,dire;//dire 1为e,2为s,3为w,4为n}rob[N];int n,m,a,b,map[N][N];int setdire(char s){switch(s){case 'E':return 1;case 'S':return 2;case 'W':return 3;case 'N':return 4;}}void Turn(Rob &rb,char s){if(s=='R'){if(rb.dire==4)    rb.dire=1;elserb.dire+=1;}else{if(rb.dire==1)    rb.dire=4;elserb.dire-=1;}}void init(){int i,x,y;char tmp[3];memset(map,0,sizeof(map));for(i=1;i<=n;i++){scanf("%d%d%s",&x,&y,tmp);map[x][y]=i;rob[i].x=x;rob[i].y=y;rob[i].dire=setdire(tmp[0]);}}void solve(){int i,j,t,num,x,y,tag=1,flag=1;char tmp;for(i=0;i<m;i++){scanf("%d %c %d",&t,&tmp,&num);if(!flag)continue;if(tmp!='F'){for(j=0;j<num%4;j++)Turn(rob[t],tmp);}else{x=rob[t].x;y=rob[t].y;if(rob[t].dire==1){for(j=1;j<=num;j++){x=rob[t].x+j;if(x>a){printf("Robot %d crashes into the wall\n",t);tag=0;flag=0;break;}if(map[x][y]){printf("Robot %d crashes into robot %d\n",t,map[x][y]);tag=0;flag=0;break;}}map[rob[t].x][rob[t].y]=0;map[rob[t].x+num][rob[t].y]=t;rob[t].x+=num;}else if(rob[t].dire==2){for(j=1;j<=num;j++){y=rob[t].y-j;if(y<1){printf("Robot %d crashes into the wall\n",t);tag=0;flag=0;break;}if(map[x][y]){printf("Robot %d crashes into robot %d\n",t,map[x][y]);tag=0;flag=0;break;}}map[rob[t].x][rob[t].y]=0;map[rob[t].x][rob[t].y-num]=t;rob[t].y-=num;}else if(rob[t].dire==3){for(j=1;j<=num;j++){x=rob[t].x-j;if(x<1){printf("Robot %d crashes into the wall\n",t);tag=0;flag=0;break;}if(map[x][y]){printf("Robot %d crashes into robot %d\n",t,map[x][y]);tag=0;flag=0;break;}}map[rob[t].x][rob[t].y]=0;map[rob[t].x-num][rob[t].y]=t;rob[t].x-=num;}else if(rob[t].dire==4){for(j=1;j<=num;j++){y=rob[t].y+j;if(y>b){printf("Robot %d crashes into the wall\n",t);tag=0;flag=0;break;}if(map[rob[t].x][y]){printf("Robot %d crashes into robot %d\n",t,map[x][y]);tag=0;flag=0;break;}}map[rob[t].x][rob[t].y]=0;map[rob[t].x][rob[t].y+num]=t;rob[t].y+=num;}}}if(tag)    printf("OK\n");}int main(int argc, char *argv[]){int t,i,j;scanf("%d",&t);while(t--){scanf("%d%d",&a,&b);scanf("%d%d",&n,&m);init();solve();}    system("PAUSE");    return EXIT_SUCCESS;}