HDU-1035,Robot Motion(DFS)

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 h 

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) 

AC Code: 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 15
char map[N][N];
int m,n,p,a,b,vis[N][N],flag,s;
void dfs(int x,int y,int step)
{
	if(x<1||x>m||y<1||y>n)//只要出界,就说明走出了迷宫 
	{
		flag=1;
		return ;
	}
	s=step;//记录走出迷宫的步数 
	if(!flag)//还未走出 
	{
		if(!vis[x][y])//这个点之前没有走过 
		{
			vis[x][y]=step;//标记路径上走到这个点时的步数 
			if(map[x][y]=='N')//四个方向 
				dfs(x-1,y,step+1);
			else if(map[x][y]=='S')
				dfs(x+1,y,step+1);
			else if(map[x][y]=='E')
				dfs(x,y+1,step+1);
			else
				dfs(x,y-1,step+1);
		}
		else//这个点之前走过 
		{
			a=s-vis[x][y];//用总步数减去之前走过这个点时的步数,就是循环圈的步数
			//对本题第二组样例,s=12,vis[x][y]=4,a=8 
			b=vis[x][y]-1;//用之前走过这个点时的步数减去刚进入迷宫的第一步,就是进入循环之前的步数
			//对本题第二组样例,b=4-1=3
		}
	}
}
int main()
{
	while(~scanf("%d %d %d",&m,&n,&p))
	{
		if(!m&&!n&&!p)
			break;
		for(int i=1;i<=m;i++)
		{
			getchar();
			for(int j=1;j<=n;j++)
			{
				scanf("%c",&map[i][j]);
			}
		}
		flag=0;
		memset(vis,0,sizeof(vis));
		dfs(1,p,1);//从第1行第p列进入,此时站在第一个点,步数为1 
		if(flag)
			printf("%d step(s) to exit\n",s);
		else
			printf("%d step(s) before a loop of %d step(s)\n",b,a);
	}
	return 0;
}
发布了302 篇原创文章 · 获赞 313 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_43823808/article/details/104342306
今日推荐