当前位置: 代码迷 >> 综合 >> HDU 1035 Robot Motion【模拟】
  详细解决方案

HDU 1035 Robot Motion【模拟】

热度:43   发布时间:2023-11-11 11:23:07.0

题目传送门;点击打开链接

Robot Motion

 
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 268(133 users) Total Accepted: 145(124 users) Rating: Special Judge: No
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
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)

这题要求你输入一个地图,该地图由四个指令E,W,S,N组成。这四个指令控制机器人向四个方移动。

问:

1:该机器人需要走多少步才能走出这个地图

2:如果该机器人走不出去,那么请问该机器人走了多少步后进入了循环??循环为多少步?


这题就是要模拟。在模拟的过程中设立一个数组来记录访问情况。如果机器人走到了访问过的点,就说明它进入了死循环。同时我们可以设立一个数组用来记录步数。那么这题就挺简单了。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define MM(a) memset(a,0,sizeof(a));
int main()
{int beg,row,cul;char Map[15][15];int  vis[15][15];///记录访问情况int  num[15][15];///记录步数while(~scanf("%d%d%d",&row,&cul,&beg)){if(row==0&&cul==0&&beg==0){break;}getchar();for(int i=1; i<=row; i++){gets(Map[i]+1);}MM(vis);MM(num);int x=1;int y=beg;char temp=Map[1][y];num[x][y]=1;///起点步数为1int test=0;int flag=1;while(1){if(vis[x][y])///如果死循环{flag=0;int temp3=test+1-num[x][y];int temp4=num[x][y]-1;printf("%d step(s) before a loop of %d step(s)\n",temp4,temp3);break;}else{vis[x][y]=1;}num[x][y]=test+1;if(temp=='N')///shang{if(x==1){break;}else{test=num[x][y];x--;}}else if(temp=='S')///xia{if(x+1>row){break;}else{test=num[x][y];x++;}}else if(temp=='W')///zuo{if(y==1){break;}else{test=num[x][y];y--;}}else if(temp=='E')///you{if(y+1>cul){break;}else{test=num[x][y];y++;}}temp=Map[x][y];}if(flag){printf("%d step(s) to exit\n",num[x][y]);}}return 0;
}//N 上
//S 下
//E 右
//W 左