Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u

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

Source

ZJCPC2004

思路:首先,本题只能用DFS不能用BFS。BFS求的是最短时间,但本题求的是在某个特定的时间能不能到某个点,如果给定的时间不是最短时间,BFS计算出的答案就是错误的,只能用DFS。接下来是剪枝,裸DFS会T掉。本题会用到以下几种剪枝:
1. 路径剪枝
1)第一次DFS之前就可以用到,地图大小是n * m,假设有wall个点是墙,可到达点的总数就是n * m – wall,如果比给定的时间少,即n * m – wall < t,则一定不能逃出去;
2)设当前走了step步,若当前位置到终点的最短路大于剩余距离,则一定不可能从当前点到终点。
2.奇偶剪枝,这里直接摘抄HDU论坛一位大牛的解题报告:http://acm.hdu.edu.cn/forum/read.php?tid=6158
把矩阵标记成如下形式:
0,1,0,1,0
1,0,1,0,1
0,1,0,1,0
1,0,1,0,1
很明显,如果起点在0 而终点在1 那显然 要经过奇数步才能从起点走到终点,依次类推,奇偶相同的偶数步,奇偶不同的奇数步
在读入数据的时候就可以判断,并且做剪枝,当然做的时候并不要求把整个矩阵0,1刷一遍,读入的时候起点记为(Si,Sj) 终点记为(Di,Dj) 判断(Si+Sj) 和 (Di+Dj) 的奇偶性就可以了
AC代码:
#include <cstdio>
#include <cstring>
using namespace std;
int n, m, t;
char map[10][10];
bool ok;
const int dx[] = {0, 0, 1, -1},
			dy[] = {1, -1, 0, 0};
struct node
{
	int x, y, step;
	bool operator == (const node b) const
	{
		return x == b.x && y == b.y && step == b.step;
	}
}start, goal;
bool input()
{
	if (scanf("%d %d %d\n", &n, &m, &t) == 3 && (n || m || t))
	{
		for (int i = 0; i < n; i++)
			scanf("%s\n", map[i]);
		return 1;
	}
	return 0;
}
int Abs(int x)
{
	if (x < 0)
		return -x;
	return x;
}
bool check(int x, int y)
{
	if (x < 0 || x >= n || y < 0 || y >= m || map[x][y] == 'X')
		return 0;
	return 1;
}
void dfs(node now)
{
	if (now.x < 0 || now.x >= n || now.y < 0 || now.y >= m)
		return;
	int res = t - now.step - Abs(goal.x - now.x) - Abs(goal.y - now.y);
	if (res < 0 || res & 1)
		return;
	if (now == goal)
	{
		ok = 1;
		return;
	}
	node next;
	next.step = now.step + 1;
	for (int i = 0; i < 4; i++)
	{
		next.x = now.x + dx[i];
		next.y = now.y + dy[i];
		if (!check(next.x, next.y))
			continue;
		map[now.x][now.y] = 'X';
		dfs(next);
		if (ok)
			return;
		map[now.x][now.y] = '.';
	}
}
void solve()
{
	ok = 0;
	int wall = 0;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			if (map[i][j] == 'S')
				start = (node){i, j, 0};
			else if (map[i][j] == 'D')
				goal = (node){i, j, t};
			else if (map[i][j] == 'X')
				wall++;
	if (n * m - wall - t < 0)
	{
		printf("NO\n");
		return;
	}
	dfs(start);
	if (ok)
		printf("YES\n");
	else
		printf("NO\n");
}
int main()
{
	while (input())
		solve();
	return 0;
}

 

分类: ACM/ICPC

0 条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注