Robot Motion
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9449 | Accepted: 4594 |
Description
![](https://i-blog.csdnimg.cn/blog_migrate/b5f7b6f7437179e2a7369804425b8270.jpeg)
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 5 NEESWE WWWESS SNWWWW 4 5 1 SESWE EESNW NWEEN EWSEN 0 0 0
Sample Output
10 step(s) to exit 3 step(s) before a loop of 8 step(s)
题目大意:
10 * 10的地图,机器人移动规则是:按照上北,下南,左西,右东进行移动,直到到达出口。入口则是第一行某一列,列数为输入第三个数据。#include <stdio.h> #include <memory.h> int mark[12][12]; char map[12][12]; int main() { int i,j,m,n,sta,x,y,row,col,count; while(scanf("%d%d%d",&m,&n,&sta) && m && n && sta) { getchar();//注意此处吸收掉一个回车 count = 0; memset(map,'O',sizeof(map));//map数组需要多加一圈'O'(大写字母O),以判断出界情况. memset(mark,0,sizeof(mark));//标记数组,用来记录某一位置走过几次 for(i=1; i<=m; i++) { for(j=1; j<=n; j++) { scanf("%c",&map[i][j]); } getchar();//注意换行出回车也需要吸收掉 } x = 1;//起始行为第一行 y = sta;//起始列为输入的sta while(1) { mark[x][y] ++;//标记数组加一,必须再条件判断之前加,否则起始位置没有加。(表示被此处坑过,一直wa) if(map[x][y] == 'N') x --; else if(map[x][y] == 'S') x ++; else if(map[x][y] == 'W') y --; else if(map[x][y] == 'E') y ++; else if(map[x][y] == 'O')//遇到出口,输出,跳出 { printf("%d step(s) to exit\n",count); break; } count ++;//计数变量加一,经验证,此处不可更换位置。 if(mark[x][y] == 2)//如果某处走过两次,说明此处是环的起始位置 { row = x;//记录此处坐标 col = y; mark[x][y] = 1;//此处走过此处置为1 x = 1;//从起点位置重新走 y = sta; count = 0;//计数变量置0 while(1) { if(x == row && y == col && mark[x][y] == 1)//进入环的起点 { printf("%d step(s) before a loop of ",count);//输出入环之前走的步数 mark[x][y] ++;//标记此位置加一 count = 0;//count置0 } if(x == row && y == col && mark[x][y] == 2 && count != 0)//再次到这个位置,即环的终点 { printf("%d step(s)\n",count);//输出环的长度 break; } //机器人的移动 if(map[x][y] == 'N') x --; else if(map[x][y] == 'S') x ++; else if(map[x][y] == 'W') y --; else if(map[x][y] == 'E') y ++; count ++;//每移动一次,计数变量加一 } break; } } } return 0; }