POJ 1573 POJ 2632(两道有趣的Robot)实例
生活随笔
收集整理的這篇文章主要介紹了
POJ 1573 POJ 2632(两道有趣的Robot)实例
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/*
** POJ 2632 Crashing Robots
** Created by Rayn @@ 2014/04/16
** 坑爹的模擬題,腦殼不清晰的就要被坑慘了
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;const int MAX = 110;struct pos{int id, dir; /* N-0,E-1,S-2,W-3 */int x, y;
};pos rob[MAX];
int HaveRob[MAX][MAX];
int K, A, B, N, M;
int mov[4][2] = {{0, 1},{1, 0},{0, -1},{-1, 0}};int DirToNum(char ch)
{switch(ch){case 'N':return 0;case 'E':return 1;case 'S':return 2;case 'W':return 3;default:break;}return -1;
}
int CrashWall(int x, int y)
{if(x<=0 || x>A || y<=0 || y>B)return 1;return 0;
}
int main()
{char str[10], act[10];int num, rep;scanf("%d", &K);while(K--){memset(HaveRob, 0, sizeof(HaveRob));scanf("%d%d%d%d", &A, &B, &N, &M);for(int i=1; i<=N; ++i){scanf("%d %d %s", &rob[i].x, &rob[i].y, str);rob[i].dir = DirToNum(str[0]);rob[i].id = i;HaveRob[rob[i].x][rob[i].y] = i;}int first = 1, ok = 1, tx, ty;while(M--){scanf("%d %s %d", &num, act, &rep);if(!first)continue;while(rep--){if(act[0] == 'L'){rob[num].dir = (rob[num].dir + 3) % 4;}if(act[0] == 'R'){rob[num].dir = (rob[num].dir + 1) % 4;}if(act[0] == 'F'){tx = rob[num].x + mov[rob[num].dir][0];ty = rob[num].y + mov[rob[num].dir][1];if(CrashWall(tx, ty) && first){printf("Robot %d crashes into the wall\n", num);first = ok = 0;}if(HaveRob[tx][ty] && first){printf("Robot %d crashes into robot %d\n", num, HaveRob[tx][ty]);first = ok = 0;}HaveRob[rob[num].x][rob[num].y] = 0;rob[num].x = tx;rob[num].y = ty;HaveRob[rob[num].x][rob[num].y] = num;}}}if(ok)printf("OK\n");}return 0;
}
/*
** POJ 2688 Cleaning Robot
** Created by Rayn @@ 2014/05/07
** 好久沒(méi)做搜索,又寫錯(cuò)了dir方向數(shù)組
** 豎向是x坐標(biāo),橫向是y坐標(biāo)
*/
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX = 15;int dir[4][2] = {{-1,0},{1,0},{0,1},{0,-1}};/* N,S,E,W */
int loop, step;
int maze[MAX][MAX], vis[MAX][MAX];void Init()
{memset(vis, 0, sizeof(vis));for(int i=0; i<MAX; ++i){for(int j=0; j<MAX; ++j){maze[i][j] = 5;?//在地圖外圈置為5,方便判斷出界}}
}
int Trans(char ch)
{int dir = 5;switch(ch){case 'N':return 0;case 'S':return 1;case 'E':return 2;case 'W':return 3;default:break;}return dir;
}
void DFS(int x, int y, int s)
{if(maze[x][y] == 5){step = s;loop = -1;return ;}if(vis[x][y] != 0){step = vis[x][y] - 1;loop = s - step;return ;}vis[x][y] = s + 1;int tx = x + dir[maze[x][y]][0];int ty = y + dir[maze[x][y]][1];DFS(tx, ty, vis[x][y]);
}
int main()
{
#ifdef _Raynfreopen("in.txt", "r",stdin);
#endifint h, w, start;char tmp[15];while(scanf("%d%d%d", &h, &w, &start) != EOF){if(h==0 && w==0 && start==0)break;Init();for(int i=1; i<=h; ++i){scanf("%s", tmp);for(int j=0; j<w; ++j){maze[i][j+1] = Trans(tmp[j]);}}loop = 0;DFS(1, start, 0);if(loop == -1)printf("%d step(s) to exit\n", step);elseprintf("%d step(s) before a loop of %d step(s)\n", step, loop);}return 0;
}
總結(jié)
以上是生活随笔為你收集整理的POJ 1573 POJ 2632(两道有趣的Robot)实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 阶乘中末尾0的个数
- 下一篇: 图结构练习——DFS——判断可达性