POJ 1573 & POJ 2632(两道有趣的Robot)

题目链接:POJ 1573 Robot Motion & POJ 2632 Crashing Robots

【题意】题意就不说了,有兴趣从链接点进去看吧,就是机器人各种打扫房间,行驶指令。

【思路】2632是一道纯模拟题,只要把题意读懂,就可以用代码模拟过程,只是写起来有点蛋疼睡觉,代码力还是欠缺啊。而1573感觉挺新奇的,我用的DFS来模拟,好久没有写DFS,一开始又把dir数组写错了抓狂,结果总是得出0。

下面贴代码,先是2632的AC代码:

  1 /*
  2 ** POJ 2632 Crashing Robots
  3 ** Created by Rayn @@ 2014/04/16
  4 ** 坑爹的模拟题,脑壳不清晰的就要被坑惨了
  5 */
  6 #include <cstdio>
  7 #include <cstring>
  8 #include <algorithm>
  9 using namespace std;
 10 
 11 const int MAX = 110;
 12 
 13 struct pos{
 14     int id, dir; /* N-0,E-1,S-2,W-3 */
 15     int x, y;
 16 };
 17 
 18 pos rob[MAX];
 19 int HaveRob[MAX][MAX];
 20 int K, A, B, N, M;
 21 int mov[4][2] = {{0, 1},{1, 0},{0, -1},{-1, 0}};
 22 
 23 int DirToNum(char ch)
 24 {
 25     switch(ch)
 26     {
 27         case 'N':
 28             return 0;
 29         case 'E':
 30             return 1;
 31         case 'S':
 32             return 2;
 33         case 'W':
 34             return 3;
 35         default:
 36             break;
 37     }
 38     return -1;
 39 }
 40 int CrashWall(int x, int y)
 41 {
 42     if(x<=0 || x>A || y<=0 || y>B)
 43         return 1;
 44     return 0;
 45 }
 46 int main()
 47 {
 48     char str[10], act[10];
 49     int num, rep;
 50 
 51     scanf("%d", &K);
 52     while(K--)
 53     {
 54         memset(HaveRob, 0, sizeof(HaveRob));
 55         scanf("%d%d%d%d", &A, &B, &N, &M);
 56         for(int i=1; i<=N; ++i)
 57         {
 58             scanf("%d %d %s", &rob[i].x, &rob[i].y, str);
 59             rob[i].dir = DirToNum(str[0]);
 60             rob[i].id = i;
 61             HaveRob[rob[i].x][rob[i].y] = i;
 62         }
 63 
 64         int first = 1, ok = 1, tx, ty;
 65         while(M--)
 66         {
 67             scanf("%d %s %d", &num, act, &rep);
 68             if(!first)
 69                 continue;
 70             while(rep--)
 71             {
 72                 if(act[0] == 'L')
 73                 {
 74                     rob[num].dir = (rob[num].dir + 3) % 4;
 75                 }
 76                 if(act[0] == 'R')
 77                 {
 78                     rob[num].dir = (rob[num].dir + 1) % 4;
 79                 }
 80                 if(act[0] == 'F')
 81                 {
 82                     tx = rob[num].x + mov[rob[num].dir][0];
 83                     ty = rob[num].y + mov[rob[num].dir][1];
 84                     if(CrashWall(tx, ty) && first)
 85                     {
 86                         printf("Robot %d crashes into the wall\n", num);
 87                         first = ok = 0;
 88                     }
 89                     if(HaveRob[tx][ty] && first)
 90                     {
 91                         printf("Robot %d crashes into robot %d\n", num, HaveRob[tx][ty]);
 92                         first = ok = 0;
 93                     }
 94                     HaveRob[rob[num].x][rob[num].y] = 0;
 95                     rob[num].x = tx;
 96                     rob[num].y = ty;
 97                     HaveRob[rob[num].x][rob[num].y] = num;
 98                 }
 99             }
100         }
101         if(ok)
102             printf("OK\n");
103     }
104     return 0;
105 }
View Code

下面是1573的AC代码:

 1 /*
 2 ** POJ 2688 Cleaning Robot
 3 ** Created by Rayn @@ 2014/05/07
 4 ** 好久没做搜索,又写错了dir方向数组
 5 ** 竖向是x坐标,横向是y坐标
 6 */
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <queue>
10 #include <algorithm>
11 using namespace std;
12 const int MAX = 15;
13 
14 int dir[4][2] = {{-1,0},{1,0},{0,1},{0,-1}};/* N,S,E,W */
15 int loop, step;
16 int maze[MAX][MAX], vis[MAX][MAX];
17 
18 void Init()
19 {
20     memset(vis, 0, sizeof(vis));
21     for(int i=0; i<MAX; ++i)
22     {
23         for(int j=0; j<MAX; ++j)
24         {
25             maze[i][j] = 5;&nbsp;//在地图外圈置为5,方便判断出界
26         }
27     }
28 }
29 int Trans(char ch)
30 {
31     int dir = 5;
32     switch(ch)
33     {
34         case 'N':
35             return 0;
36         case 'S':
37             return 1;
38         case 'E':
39             return 2;
40         case 'W':
41             return 3;
42         default:
43             break;
44     }
45     return dir;
46 }
47 void DFS(int x, int y, int s)
48 {
49     if(maze[x][y] == 5)
50     {
51         step = s;
52         loop = -1;
53         return ;
54     }
55     if(vis[x][y] != 0)
56     {
57         step = vis[x][y] - 1;
58         loop = s - step;
59         return ;
60     }
61     vis[x][y] = s + 1;
62     int tx = x + dir[maze[x][y]][0];
63     int ty = y + dir[maze[x][y]][1];
64     DFS(tx, ty, vis[x][y]);
65 }
66 int main()
67 {
68 #ifdef _Rayn
69     freopen("in.txt", "r",stdin);
70 #endif
71 
72     int h, w, start;
73     char tmp[15];
74 
75     while(scanf("%d%d%d", &h, &w, &start) != EOF)
76     {
77         if(h==0 && w==0 && start==0)
78             break;
79         Init();
80         for(int i=1; i<=h; ++i)
81         {
82             scanf("%s", tmp);
83             for(int j=0; j<w; ++j)
84             {
85                 maze[i][j+1] = Trans(tmp[j]);
86             }
87         }
88         loop = 0;
89         DFS(1, start, 0);
90         if(loop == -1)
91             printf("%d step(s) to exit\n", step);
92         else
93             printf("%d step(s) before a loop of %d step(s)\n", step, loop);
94     }
95     return 0;
96 }
View Code

 

posted @ 2014-05-16 12:22  Rayn  阅读(145)  评论(0编辑  收藏  举报