POJ

来源:互联网 发布:p2p理财软件 编辑:程序博客网 时间:2024/05/22 12:42


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
3 6 5NEESWEWWWESSSNWWWW4 5 1SESWEEESNWNWEENEWSEN0 0 0
Sample Output
10 step(s) to exit3 step(s) before a loop of 8 step(s)

思路:把走过的点step记录一下,ans记录当前步数,一旦重复,就可以输出了,不重复就一定能走出,输出ans即可;

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#define max_n 1010using namespace std;char mapp[max_n][max_n];int visit[max_n][max_n];int n,m,k;struct node{int x;int y;int step;bool friend operator <(node a,node b){return a.step>b.step;}};void bfs(int p1,int p2){bool flag=false;int ans=0;priority_queue<node> q;node now,next;now.x=p1;now.y=p2;now.step=0;q.push(now);while(!q.empty()){next=q.top();q.pop();if(next.x<1 || next.x>n || next.y<1 || next.y>m){printf("%d step(s) to exit\n",ans);return;}if(mapp[next.x][next.y]=='N'){now.x=next.x-1;now.y=next.y;now.step=next.step+1;if(visit[next.x][next.y]){printf("%d step(s) before a loop of %d step(s)\n",visit[next.x][next.y]-1,ans-visit[next.x][next.y]+1);return;}visit[next.x][next.y]=ans+1;}if(mapp[next.x][next.y]=='S'){now.x=next.x+1;now.y=next.y;now.step=next.step+1;if(visit[next.x][next.y]){printf("%d step(s) before a loop of %d step(s)\n",visit[next.x][next.y]-1,ans-visit[next.x][next.y]+1);return;}visit[next.x][next.y]=ans+1;}if(mapp[next.x][next.y]=='E'){now.x=next.x;now.y=next.y+1;now.step=next.step+1;if(visit[next.x][next.y]){printf("%d step(s) before a loop of %d step(s)\n",visit[next.x][next.y]-1,ans-visit[next.x][next.y]+1);return;}visit[next.x][next.y]=ans+1;}if(mapp[next.x][next.y]=='W'){now.x=next.x;now.y=next.y-1;now.step=next.step+1;if(visit[next.x][next.y]){printf("%d step(s) before a loop of %d step(s)\n",visit[next.x][next.y]-1,ans-visit[next.x][next.y]+1);return;}visit[next.x][next.y]=ans+1;}q.push(now);ans++;}}int main(){while(scanf("%d %d %d",&n,&m,&k) && (n+m+k)){memset(visit,0,sizeof(visit));for(int i=1;i<=n;i++){getchar();for(int j=1;j<=m;j++){scanf("%c",&mapp[i][j]);}}bfs(1,k);}return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 两岁宝宝喘气粗怎么办 两岁宝宝断不了奶怎么办 两岁宝宝不愿意喝奶粉怎么办 吃母乳不愿意吃奶粉怎么办 母乳宝宝不愿意喝奶粉怎么办 宝宝断母乳不喝奶粉怎么办 9个月宝宝不会爬怎么办 孩子五年级学习成绩差该怎么办 孩子临近中考学习成绩很差该怎么办 初中生成绩不好家长该怎么办 成绩差该怎么办贴吧 宝宝只会匍匐爬怎么办 一年级的孩子数学不好怎么办 小学一年级孩子数学不好怎么办 孩子上一年级数学太差怎么办 智商低情商也低怎么办? 孩子字写得很大怎么办 孩子拿笔重 写字太黑 怎么办 孩子语文成绩好数学不行怎么办? 孩子现在二年级特别叛逆怎么办 孩子又笨又蠢怎么办 四年级孩子数学计算能力差怎么办 孩子四年级数学理解能力差怎么办 孩子小学四年级数学很差怎么办 孩子做作业太慢怎么办 小学三年级数学成绩差怎么办 初一数学考了3分怎么办 初二物理太差该怎么办 三年级孩子字写的差怎么办 小学三年级数学才考86怎么办 小孩子一发脾气就打妈妈怎么办 孩子做错事不肯道歉怎么办 小孩写作业注意力不集中怎么办 六年级的数学下册差怎么办 一年级小孩做作业慢怎么办 静不下心写作业怎么办 二年级应用题太差怎么办 小学二年级数学差怎么办 小学二年级成绩差怎么办 6个月小孩爱动怎么办 儿子叛逆期我该怎么办