(HDU1010)Tempter of the Bone-DFS

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 147039    Accepted Submission(s): 39216


 

Problem Description

The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

 

Input

The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.

The input is terminated with three 0's. This test case is not to be processed.

 

Output

For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.

 

Sample Input

 

4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0

 

Sample Output

 

NO YES

题目分析:

给一个图,问能能否从起点恰好在第t步到达终点。没什么好说的,就从起点开始向四个方向深搜,判断当前点是不是终点(是否在第t步恰好到达),唯一需要注意和学习的就是奇偶剪枝。奇偶剪枝另一篇文章有写,就不多说了。

代码:

#include<iostream>
#include<string.h>
using namespace std;

struct Dir {
	int x;
	int y;
	Dir(int x1, int y1) { x = x1; y = y1; }
};

int N, M, T;
char maze[8][8];		//地图
int sx, sy,ex,ey;		//起点终点坐标
int visit[8][8];
int flag = 0;
Dir point[4] = { {0,1},{1,0},{0,-1},{-1,0} };  //四个方向
void DFS(int x, int y,int sum) {
	
	if (x<1 || x>N || y<1 || y>M || maze[x][y] == 'X'||flag == 1)return;
	if (maze[x][y] == 'D') {
		if (sum==T) {
			flag = 1;
		}
		return;
	}
	for (int i = 0; i < 4; i++) {
		if (!visit[x + point[i].x][y + point[i].y]) {
			visit[x + point[i].x][y + point[i].y] = 1;
			DFS(x + point[i].x, y + point[i].y, sum+1);
			visit[x + point[i].x][y + point[i].y] = 0;
		}
	}
}


int main() {
	while (cin >> N >> M >> T&&N&&M&&T) {
		flag = 0;
		memset(visit, 0, sizeof(visit));
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= M; j++) {
				cin >> maze[i][j];
				if (maze[i][j] == 'S') {
					sx = i; sy = j;
				}
				if(maze[i][j] == 'D') {
					ex = i; ey = j;
				}
			}
		}
		int dis = abs(sx - ex) + abs(sy - ey);//奇偶剪枝
		if (dis > T||dis%2!=T%2) {
			cout << "NO" << endl;
			continue;
		}
		visit[sx][sy] = 1;
		DFS(sx, sy, 0);
		if (flag == 1)cout << "YES" << endl;
		else cout << "NO" << endl;
	}
	return 0;
}

猜你喜欢

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