您的位置:首页 > 其它

hdoj 1035 Robot Motion 【DFS 暴力】

2015-06-23 22:01 309 查看


Robot Motion

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7965 Accepted Submission(s): 3680



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)



题意:给出一个n*m矩阵和机器人的起点,起点为第一行第s列的点。 矩阵上有4种字符,其中当机器人走到N时,表明机器人需要向上走一步;S 向下走一步; E 向右走一步 ;W向左走一步。 当机器人走出矩阵 或者 走的路径成环时结束(两种情况成立一种就行),然后输出机器人走出矩阵的步数 或者 成环前走的步数以及环的步数(对应上面成立的情况)。

思路:DFS 没什么好解释的,row <= 10, cul <= 10。 果断不会超时。我用rec[x][y] 记录第一次走到(x, y)的步数,初始化为-1。其中越界时好处理,直接输出即可;当处理成环时,若对应的rec[x][y]不为-1时,说明该坐标已走过,rec[x][y]就是成环前的步数,而当前步数减去rec[x][y]就是环的步数。详情看代码。

0ms:

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
char Map[11][11];//图 
int rec[11][11];//记录步数 
int n, m, s;
void init()//初始化 
{
	memset(rec, -1, sizeof(rec));
}
void getMap()
{
	for(int i = 0; i < n; i++)
	scanf("%s", Map[i]); 
}
bool judge(int x, int y)
{
	return x >= 0 && x < n && y >= 0 && y < m; 
}
void DFS(int x, int y, int step)//当前坐标, 已走的步数
{
	if(!judge(x, y))//越界出去
	{
		printf("%d step(s) to exit\n", step);
		return ;
	} 
	if(rec[x][y] != -1)//已经查到 成环 
	{
		printf("%d step(s) before a loop of %d step(s)\n", rec[x][y], step-rec[x][y]); 
		return ;
	}
	else
	rec[x][y] = step;//记录步数 
	switch(Map[x][y])
	{
		case 'N': DFS(x-1, y, step+1); break;//步数注意加一
		case 'S': DFS(x+1, y, step+1); break;
		case 'E': DFS(x, y+1, step+1); break;
		case 'W': DFS(x, y-1, step+1); break;
	}
}
int main()
{
	while(scanf("%d%d", &n, &m), n||m)
	{
		scanf("%d", &s);
		init();
		getMap();
		DFS(0, s-1, 0);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: