hdoj 1035 Robot Motion
Robot Motion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1287 Accepted Submission(s): 592
Problem 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 5NEESWEWWWESSSNWWWW4 5 1SESWEEESNWNWEENEWSEN0 0
Sample Output
10 step(s) to exit3 step(s) before a loop of 8 step(s)
//题意:给出机器人所在的方向矩阵以及开始位置,然后求最后机器人是走出矩阵还是在矩阵中循环走动。
//思路:用简单的递归,按照矩阵中的方向走,走出矩阵或者循环时就输出结果。
#include <iostream>
using namespace std;
char a[15][15];
int f[15][15],r,c;
void path(int er,int ec,int step)
{//递归函数
if( er<r && er>=0 && ec<c && ec>=0 )
{//判断机器人是否还在矩阵范围内
if( f[er][ec]==0 )//这个点没走过
{//f[er][ec]记录机器人走到(er,ec)这点时所用的步数
f[er][ec]=++step;//step记录总步数
switch (a[er][ec])
{
case 'S':
path(er+1,ec,step); break;
case 'N':
path(er-1,ec,step); break;
case 'E':
path(er,ec+1,step); break;
case 'W':
path(er,ec-1,step); break;
default:
break;
}
}
else//如果回到曾经走过的点,则表示循环
cout<<f[er][ec]-1<<" step(s) before a loop of "<<step-f[er][ec]+1<<" step(s)"<<endl;
}
else//不满足条件,则表示走出了矩阵
cout<<step<<" step(s) to exit"<<endl;
}
int main()
{
//freopen("acm.txt","r",stdin);
int e,i,j,step;
while( cin>>r>>c>>e )
{
if( r==0 && c==0 && e==0 )
break;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>a[i][j];
}
memset(f,0,sizeof(f));//用来记录步数的 f[][] 致 0
step=0;
path(0,e-1,step);//递归找出路
}
return 0;
}

浙公网安备 33010602011771号