Robot Motion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13104 Accepted Submission(s): 6104
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 5 NEESWE WWWESS SNWWWW 4 5 1 SESWE EESNW NWEEN EWSEN 0 0
第一次没看题解过了一道像样的题目...
这个题直接两次DFS即可,就是if语句有点多,写的我想吐...
无非两种情况(是否有环(开一个vis数组判断即可));
DFS1:到达边界时终止,分组讨论。如果有环记录下来位置,走的距离为Step1
DFS2:有环的时候使用,从起点走到终点,走的距离为step2.
如果无环,ans=step1+1;
否则环的长度为step1-step2;
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define MAXN 1050
char maze[MAXN][MAXN];
int vis[MAXN][MAXN];
int ex,ey;
int step1;
int step2;
int n,m,start;
void dfs1(int nx,int ny)
{
if((nx==1&&maze[nx][ny]=='N'&&ny>1&&ny<m)||(ny==1&&maze[nx][ny]=='W'&&nx>1&&nx<n)||(ny==m&&maze[nx][ny]=='E'&&nx>1&&nx<n)||(nx==n&&ny>1&&ny<m&&maze[nx][ny]=='S')||(nx
==1&&ny==1&&(maze[nx][ny]=='N'||maze[nx][ny]=='W'))||(nx==1&&ny==m&&(maze[nx][ny]=='N'||maze[nx][ny]=='E'))||(nx==n&&ny==1&&(maze[nx][ny]=='S'||maze[nx][ny]=='W'))
||(nx==n&&ny==m)&&(maze[nx][ny]=='S'||maze[nx][ny]=='E'))
{
return ;
}
if(vis[nx][ny]==1)
{
vis[nx][ny]=2;
return ;
}
vis[nx][ny]=1;
step1++;
if(maze[nx][ny]=='W'&&nx>=1&&nx<=n&&ny>=1&&ny<=m) dfs1(nx,ny-1);
else if(maze[nx][ny]=='N'&&nx>=1&&nx<=n&&ny>=1&&ny<=m) dfs1(nx-1,ny);
else if(maze[nx][ny]=='E'&&nx>=1&&nx<=n&&ny>=1&&ny<=m) dfs1(nx,ny+1);
else if(maze[nx][ny]=='S'&&nx>=1&&nx<=n&&ny>=1&&ny<=m) dfs1(nx+1,ny);
}
void dfs2(int nx,int ny)
{
if(nx==ex&&ny==ey)
{
return ;
}
step2++;
if(maze[nx][ny]=='W'&&nx>=1&&nx<=n&&ny>=1&&ny<=m) dfs2(nx,ny-1);
else if(maze[nx][ny]=='N'&&nx>=1&&nx<=n&&ny>=1&&ny<=m) dfs2(nx-1,ny);
else if(maze[nx][ny]=='E'&&nx>=1&&nx<=n&&ny>=1&&ny<=m) dfs2(nx,ny+1);
else if(maze[nx][ny]=='S'&&nx>=1&&nx<=n&&ny>=1&&ny<=m) dfs2(nx+1,ny);
}
int main()
{
while(cin>>n>>m&&n&&m)
{
cin>>start;
step1=0;
step2=0;
if(n+m+start==0) break;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>maze[i][j];
}
}
dfs1(1,start);
int flag=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(vis[i][j]==2)
{
ex=i;
ey=j;
flag=1;
break;
}
}
}
if(flag)
{
dfs2(1,start);
cout<<step2<<" step(s) before a loop of "<<step1-step2<<" step(s)"<<endl;
}
else cout<<step1+1<<" step(s) to exit"<<endl;
}
return 0;
}