标签:hdu1035 hdu 1035 dfs
Robot Motion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8180 Accepted Submission(s): 3771
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
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)
Source
Recommend
We have carefully selected several similar problems for you:
1010 2553 1258 1045 2660
Statistic | Submit | Discuss | Note
前几天看到这道题 感觉看到英文就头大了。。就没做。。
o(︶︿︶)o 唉 迟早都要做了。。刚就看了看 一个DFS而已、、
主要就判断环的地方。用一个vis数组标记一下 并且用vis数组存贮到当前位置需要的步数
#include <stdio.h>
#include <string.h>
char map[15][15];
int sum,m,n,flag,mark,mark_x,mark_y,vis[15][15];
void bfs(int x,int y,int ant)
{
if(x<0||y<0||x==m||y==n)//如果出界 就证明能够出去了
{
sum=ant;
return ;
}
if(vis[x][y])//自身成环 记录目前的步数和坐标
{
flag=1;
mark_x=x,mark_y=y;
mark=ant;
return ;
}
vis[x][y]=ant+1;
if(map[x][y]=='W'&&!sum&&!flag)
bfs(x,y-1,++ant);
if(map[x][y]=='E'&&!sum&&!flag)
bfs(x,y+1,++ant);
if(map[x][y]=='N'&&!sum&&!flag)
bfs(x-1,y,++ant);
if(map[x][y]=='S'&&!sum&&!flag)
bfs(x+1,y,++ant);
}
int main()
{
int s;
while(scanf("%d %d %d",&m,&n,&s)!=EOF)
{
if(m==0&&n==0&&s==0)
break;
for(int i=0;i<m;i++)
scanf("%s",map[i]);
sum=flag=0;
memset(vis,0,sizeof(vis));
bfs(0,s-1,0);
if(!flag)
printf("%d step(s) to exit\n",sum);
else
printf("%d step(s) before a loop of %d step(s)\n",vis[mark_x][mark_y]-1,mark-vis[mark_x][mark_y]+1);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
hdu1035 Robot Motion (DFS)
标签:hdu1035 hdu 1035 dfs
原文地址:http://blog.csdn.net/su20145104009/article/details/47295269