Robot Motion
Description A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are N north (up the page) S south (down the page) E east (to the right on the page) W west (to the left on the page) For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid. Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits. You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around. Input There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0. Output For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1. Sample Input Sample Output Source |
[Submit] [Go Back] [Status] [Discuss
题目大意:一个机器人按图中所给的四种方向移动,如过能走出去问需要几步,如果不能问在进入循环前走了几步,进入循环后又走了几步。
思路:bfs模版题,同时要注意用一个数组记录一下机器人每到达一个点走了多少步。
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=1005;
int row,column,enter;
int sx,sy;
char mp[MAXN][MAXN];
int vis[MAXN][MAXN],stepp[MAXN][MAXN];
struct node{
int x,y,step;
bool friend operator <(node a,node b){
return a.step>b.step;
}
};
void BFS(){
memset(vis,0,sizeof(vis));
priority_queue<node> qu;
node e1;
e1.x=sx,e1.y=sy;
qu.push(e1);
//vis[e1.x][e1.y]=1;
int ans=-1;
while(!qu.empty()){
e1=qu.top();qu.pop();
node e2;
ans++;
if(e1.x<0||e1.x>=row||e1.y<0||e1.y>=column){
//ans=e1.step;
printf("%d step(s) to exit\n",ans);
break;
}
if(vis[e1.x][e1.y]){
printf("%d step(s) before a loop of %d step(s)\n",stepp[e1.x][e1.y],ans-stepp[e1.x][e1.y]);
break;
}
vis[e1.x][e1.y]=1;
stepp[e1.x][e1.y]=ans;
if(mp[e1.x][e1.y]=='W'){
e2.x=e1.x;
e2.y=e1.y-1;
e2.step=e1.step+1;
//vis[e2.x][e2.y]=1;
qu.push(e2);
}
else if(mp[e1.x][e1.y]=='E'){
e2.x=e1.x;
e2.y=e1.y+1;
e2.step=e1.step+1;
//vis[e2.x][e2.y]=1;
qu.push(e2);
}
else if(mp[e1.x][e1.y]=='S'){
e2.x=e1.x+1;
e2.y=e1.y;
e2.step=e1.step+1;
//vis[e2.x][e2.y]=1;
qu.push(e2);
}
else if(mp[e1.x][e1.y]=='N'){
e2.x=e1.x-1;
e2.y=e1.y;
e2.step=e1.step+1;
//vis[e2.x][e2.y]=1;
qu.push(e2);
}
}
}
int main(){
while(~scanf("%d%d%d",&row,&column,&enter)&&row,column,enter){
sx=0,sy=enter-1;
for(int i=0;i<row;i++) scanf("%s",mp[i]);
BFS();
}
return 0;
}