POJ 1573 Robot Motion 搜索找一个循环
2017-10-31 17:44
387 查看
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 12845 | Accepted: 6234 |
3 6 5 NEESWE WWWESS SNWWWW 4 5 1 SESWE EESNW NWEEN EWSEN 0 0 0Sample Output
10 step(s) to exit 3 step(s) before a loop of 8 step(s)
这个我们需要一个二维数组记录每个节点第一次到达时所用的步数,这样我们如果到达一个已经访问过的点,那么我们一定是绕圈子了。
同时我们们要记录走到当前点走了几步了,那么圈子大小就是当前点走了几步减去第一次到达时走了几步。
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; struct node { int x,y; node(int x=0,int y=0):x(x),y(y) {}; //装X的写法 }; char a[15][15]; bool vis[15][15]; int step[15][15]; int n,m,s; void BFS() { queue<node>q; memset(vis,false,sizeof(vis)); step[0][s-1]=0; q.push(node(0,s-1)); int stepp=-1; while(!q.empty()) { node now=q.front(); q.pop(); stepp++; if(now.x<0||now.x>=n||now.y<0||now.y>=m) { printf("%d step(s) to exit\n",stepp); return ; } if(vis[now.x][now.y]) { printf("%d step(s) before a loop of %d step(s)\n",step[now.x][now.y],stepp-step[now.x][now.y]); return ; } vis[now.x][now.y]=true; step[now.x][now.y]=stepp; if(a[now.x][now.y]=='S') { q.push(node(now.x+1,now.y)); } else if(a[now.x][now.y]=='N') { q.push(node(now.x-1,now.y)); } else if(a[now.x][now.y]=='W') { q.push(node(now.x,now.y-1)); } else if(a[now.x][now.y]=='E') { q.push(node(now.x,now.y+1)); } } } int main() { while(cin>>n>>m>>s,n,m,s) { for(int i=0; i<n; i++) cin>>a[i]; BFS(); } return 0; }
相关文章推荐
- poj 1573 Robot Motion【模拟题 写个while循环一直到机器人跳出来】
- POJ 1573 Robot Motion【是搜索,就不要纠结是DFS还是BFS】
- poj 1573 Robot Motion
- POJ 1573 Robot Motion(BFS)
- POJ 1573 Robot Motion 模拟机器人行走
- Robot Motion--POJ 1573
- poj 1573 Robot Motion
- poj 1573(搜索)
- POJ训练计划1573_Robot Motion(递归)
- POJ 1573 Robot Motion
- poj 1573 Robot Motion
- Robot Motion(POJ 1573 模拟题)
- poj 2632 Crashing Robots&&POJ 1573 Robot Motion 【建坐标系的问题】
- POJ 1573:Robot Motion
- POJ 1573 Robot Motion (DFS)
- POJ-1573-Robot Motion
- POJ 一 1573 Robot Motion
- POJ 1573 && hdu 1035 Robot Motion【简单模拟】
- Poj OpenJudge 百练 1573 Robot Motion
- Robot Motion(POJ 1573)(水题+纯模拟)