poj 2632 Crashing Robots

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

题意:已知仓库大小,告诉机器人的起始位置和方向,告诉m条指令,求按指令会不会撞墙或撞机器人。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 int map[110][110];
 9 int dx[5]={0,1,0,-1};
10 int dy[5]={1,0,-1,0};
11 struct node
12 {
13      int x,y,d;//d与dx,dy数组记录方向
14 }rob[1000];
15 int main()
16 {
17    int t,n,m,a,b,i;
18    int f,r,rep,r1,r2;
19    char act,ch;
20    cin>>t;
21    while(t--)
22    {
23        f=0;
24        memset(map,0,sizeof(map));
25        cin>>a>>b>>n>>m;
26        for(i=1; i<=n; i++)
27        {
28            cin>>rob[i].x>>rob[i].y>>ch;
29            map[rob[i].x][rob[i].y]=i;//map用坐标记录机器人的位置
30            if(ch=='N') rob[i].d=0;
31            if(ch=='E') rob[i].d=1;
32            if(ch=='S') rob[i].d=2;
33            if(ch=='W') rob[i].d=3;
34        }
35        while(m--)
36        {
37            cin>>r>>act>>rep;
38            if(f==0)
39            {
40                if(act=='L') rob[r].d=((rob[r].d-rep)%4+4)%4;
41                else if(act=='R') rob[r].d=(rob[r].d+rep)%4;
42             else
43             {
44                 map[rob[r].x][rob[r].y]=0;
45                 for(i=0; i<rep; i++)
46                 {
47                     rob[r].x+=dx[rob[r].d];
48                     rob[r].y+=dy[rob[r].d];
49                     if(rob[r].x<=0 || rob[r].y<=0 || rob[r].x>a ||rob[r].y>b)
50                     {
51                         r1=r;
52                         f=1;
53                         break;
54                     }
55                     else if(map[rob[r].x][rob[r].y])
56                     {
57                         r1=r;
58                         f=2;
59                         r2=map[rob[r].x][rob[r].y];
60                         break;
61                     }
62                 }
63                 if(f==0)
64                map[rob[r].x][rob[r].y]=r;
65             }
66            }
67        }
68        if(f==1) printf("Robot %d crashes into the wall
", r1);
69        else if(f==2) printf("Robot %d crashes into robot %d
", r1,r2);
70        else printf("OK
");
71    }
72     return 0;
73 }
原文地址:https://www.cnblogs.com/bfshm/p/3227867.html