(POJ1573)Robot Motion-DFS

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 0

Sample Output

10 step(s) to exit
3 step(s) before a loop of 8 step(s)

题目分析:

题目大意是说根据字母的不同,人接下来会向不同的方向行走,如果能走出去,输出走了多少步;如果有循环,输出进循环前走了多少步和循环的步数。图的搜索,只不过每次向哪个方向DFS需要通过当前位置的字母决定。

#include<iostream>
using namespace std;
int r, c, ent;
char mapp[11][11];
int visit[11][11];		//标记走到当前位置走了多少步,若没走过为0
int loop;			//记录是否有循环
int DFS(int x, int y,int &num) {
	if (x < 0 || x == r || y < 0 || y == c)return 0;	//出界说明走出去了,退出
	if (visit[x][y]) {				//当前点走过说明有循环,记录当前点步数然后退出
		loop = visit[x][y];
		return 0;
	}
	num = num + 1;
	visit[x][y] = num;
	//根据当前位置的字母是什么来决定下一步向哪个方向走
	if (mapp[x][y] == 'N') { DFS(x-1,y,num); }
	if (mapp[x][y] == 'S') { DFS(x + 1,y,num); }
	if (mapp[x][y] == 'E') { DFS(x, y + 1,num); }
	if (mapp[x][y] == 'W') { DFS(x, y - 1,num); }
}

int main() {
	
	while (cin >> r >> c >> ent&&r&&c&&ent) {
		memset(visit, 0, sizeof(visit));
		loop = 0;
		for (int i = 0; i < r; i++) {
			cin >> mapp[i];
		}
		int num = 0;
		DFS(0, ent-1,num);
		if (loop)cout << loop - 1 << " step(s) before a loop of " << num - loop+1 << " step(s)" << endl;	//有循环输出
		else cout << num << " step(s) to exit" << endl;		//无循环输出

	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xiaosi1524/article/details/81196193