Robot Motion------[NWPU][2018寒假作业][通用版]一F题
2018-01-27 11:19
411 查看
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
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)
解题思路:
用switch结构,实现向不同方向走的这个过程。
并且用一个int型二维数组记录在某个位置已经走了多少步,如果最后走到边界则为exit情况;如果走到一个步数不为0的位置,证明已经走过这个位置,将进入循环,是loop情况。
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
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)
解题思路:
用switch结构,实现向不同方向走的这个过程。
并且用一个int型二维数组记录在某个位置已经走了多少步,如果最后走到边界则为exit情况;如果走到一个步数不为0的位置,证明已经走过这个位置,将进入循环,是loop情况。
#include<stdio.h> int main() { int row,col,ent; char grid[11][11]={0}; int count[11][11]={0}; char flag; int x,y,cnt; char temp; while(1) { scanf("%d%d%d",&row,&col,&ent); if(row==0||col==0||ent==0) return 0; for(int i=1;i<=row;i++) { temp=getchar(); for(int j=1;j<=col;j++) { scanf("%c",&grid[i][j]); count[i][j]=0; } } x=1; y=ent; cnt=0; while(1) { flag=grid[x][y]; count[x][y]=++cnt; switch(flag) { case 'N': x=x-1; break; case 'S': x=x+1; break; case 'E': y=y+1; break; case 'W': y=y-1; break; } if(x<1||x>row||y<1||y>col) { printf("%d step(s) to exit\n",cnt); break; } if(count[x][y]!=0) { printf("%d step(s) before a loop of %d step(s)\n",count[x][y]-1,cnt-count[x][y]+1); break; } } } }
相关文章推荐
- Digital root------[NWPU][2018寒假作业][通用版]一、热身 [Cloned]M题//求数字根的简便方法
- Dirichlet's Theorem on Arithmetic Progressions------[NWPU][2018寒假作业][通用版]一、热身 [Cloned]O题
- Ignatius and the Princess IV ------[NWPU][2018寒假作业][通用版]一、热身 [Cloned]J题
- Joseph------[NWPU][2018寒假作业][通用版]一、热身 [Cloned]R题
- A + B ------[NWPU][2018寒假作业][通用版]一、热身 [Cloned]E题
- Candy Sharing Game------[NWPU][2018寒假作业][通用版]一、热身 [Cloned]B题
- //switch结构//Edge------[NWPU][2018寒假作业][通用版]一C题
- HDU2054 A==B------ [NWPU][2018寒假作业][通用版]一、热身 [Cloned]Y题
- //递推//折线分割平面------[NWPU][2018寒假作业][通用版]二F题
- Hero贪心2------[NWPU][2018寒假作业][通用版]二Q题
- stl的map1------[NWPU][2018寒假作业][通用版]二、stl ,模拟,贪心等 [Cloned]A题
- stl的map2------[NWPU][2018寒假作业][通用版]二、stl ,模拟,贪心等 [Cloned]B题
- //动能定理和弹性势能//James Bond------[NWPU][2018寒假作业][通用版]二D题
- //为什么一直PE?//经营水果商店------[NWPU][2018寒假作业][通用版]二C题
- Doing Homework Again贪心------[NWPU][2018寒假作业][通用版]二、stl ,模拟,贪心等 [Cloned]P题
- //搞不清楚for循环真的害死人//贪心FatMouse's Trade------[NWPU][2018寒假作业][通用版]二E题
- 2018寒假作业_3(电梯版本二)
- 2018寒假作业4
- 2018寒假作业_1(时光荏苒 师情难忘)
- 2018寒假作业3