首页 > 代码库 > POJ 2632 Crashing Robots(较为繁琐的模拟)

POJ 2632 Crashing Robots(较为繁琐的模拟)

题目链接:http://poj.org/problem?id=2632

题目大意:题意简单,N个机器人在一个A*B的网格上运动,告诉你机器人的起始位置和对它的具体操作,输出结果:

1.Robot i crashes into the wall, if robot i crashes into a wall. (A robot crashes into a wall if Xi = 0, Xi = A + 1, Yi = 0 or Yi = B + 1.) 撞墙

2.Robot i crashes into robot j, if robots i and j crash, and i is the moving robot. 两个机器人相撞

3.OK, if no crashing occurs.没有发生任何碰撞

思路:模拟模拟~~

用一个结构体变量记录每个robet的信息。。具体看代码吧:

  1 #include<iostream>  2 #include<fstream>  3 using namespace std;  4 struct node  5 {  6     int id;  7     int x;//机器人的坐标  8     int y;  9     char ch;//机器人的方向 10 }robet[102]; 11 int main() 12 { 13     int T; 14     scanf("%d",&T); 15     while(T--) 16     { 17         int X,Y; 18         cin>>X>>Y; 19         int numrobet,times; 20         cin>>numrobet>>times; 21         int i; 22         for(i=1; i<=numrobet; i++) 23         { 24             robet[i].id=i; 25             cin>>robet[i].x>>robet[i].y>>robet[i].ch;//机器人的初始位置及朝向 26         } 27         int id,step,j,tage=0,k,r; 28         char direction; 29         for(i=1;i<=times;i++) 30         { 31             cin>>id>>direction>>step;//编号为id的机器人的操作和重复操作的次数 32             for(j=1;j<=numrobet;j++) 33             { 34                 if(robet[j].id==id) 35                 { 36                     for(k=0;k<step;k++) 37                     { 38                         if(tage==1) 39                             break; 40                         else if(tage==0)//机器人的位置和方向的改变 41                         { 42                             if(robet[j].ch==N)//如果机器人一开始朝北 43                             { 44                                 if(direction==F)//操作“F”,向北进一 45                                     robet[j].y+=1; 46                                 else if(direction==L)//操作“L”,向左转 47                                     robet[j].ch=W; 48                                 else if(direction==R)//操作“R”,向右转 49                                     robet[j].ch=E; 50                             } 51                             else if(robet[j].ch==E) 52                             { 53                                 if(direction==F) 54                                     robet[j].x+=1; 55                                 else if(direction==L) 56                                     robet[j].ch=N; 57                                 else if(direction==R) 58                                     robet[j].ch=S; 59                             } 60                             else if(robet[j].ch==W) 61                             { 62                                 if(direction==F) 63                                     robet[j].x-=1; 64                                 else if(direction==L) 65                                     robet[j].ch=S; 66                                 else if(direction==R) 67                                     robet[j].ch=N; 68                             } 69                             else if(robet[j].ch==S) 70                             { 71                                 if(direction==F) 72                                     robet[j].y-=1; 73                                 else if(direction==L) 74                                     robet[j].ch=E; 75                                 else if(direction==R) 76                                     robet[j].ch=W; 77                             } 78                         } 79                         if(robet[id].x<=0||robet[id].y<=0||robet[id].x>X||robet[id].y>Y) 80                         { 81                             cout<<"Robot "<<id<<" crashes into the wall"<<endl; 82                             tage=1; 83                             break; 84                         }//判断撞墙 85                         else 86                         { 87                             for(r=1; r<=numrobet; r++) 88                             { 89                                 if(robet[r].x==robet[id].x&&robet[r].y==robet[id].y&&r!=id) 90                                 { 91                                     cout<<"Robot "<<id<<" crashes into robot "<<r<<endl; 92                                     tage=1; 93                                 } 94                             } 95                             if(tage==1) 96                                 break; 97                         }//判断两个机器人相撞 98                     } 99                 }100             }101         }102         if(tage==0)103             cout<<"OK"<<endl;104     }105     return 0;106 }

 

POJ 2632 Crashing Robots(较为繁琐的模拟)