杭电1010,深度优先搜索+减枝

来源:互联网 发布:英文域名注册查询 编辑:程序博客网 时间:2024/05/01 04:13

使用深度优先搜索,但不是判断能否搜到,也不是找最长路径,而是判断是否有一条刚好长度为T的路。

递归实现(虽然很容易超时)

链接:hdu1010

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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
 

Sample Output
NOYES
#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <cstring>#include <climits>#include <stack>#include <queue>#include <vector>#include <string>#include <map>#include <set>#include <algorithm>using namespace std;const int maxn = 10;struct Node {int x, y;bool operator == (Node& rhs) const {if (rhs.x == x && rhs.y == y)return true;return false;} }start;int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; //上下左右Node nd; //命名为end报错,说有歧义int N, M, T;char pic[maxn][maxn];bool DFS(Node v, int d) { // distanceif (d == T && v == nd)return true;if (0 > v.x || v.x >= N || 0 > v.y || v.y > M)return false; // 其实根本不会越界,因为在搜索前就判定了//根据奇偶性减枝    int tmp = T - d - abs(nd.x - v.x) - abs(nd.y - v.y);if (tmp < 0 || (tmp & 1) == 1) // tmp = 0 时,很可能有解return false;for (int i = 0; i < 4; ++i) {int xx = v.x + dir[i][0], yy = v.y + dir[i][1];if (0 <= xx && xx < N && 0 <= yy && yy < M && pic[xx][yy] != 'X') {pic[xx][yy] = 'X';if (DFS({ xx, yy }, d + 1))return true;//重新设置成false,因为它有可能出现在下一次搜索的别的路径中pic[xx][yy] = '.'; }}return false;}int main() {while (~scanf("%d%d%d", &N, &M, &T)) {if (N == 0 && M == 0 && T == 0)break;int wall = 0;for (int i = 0; i < N; ++i)for (int j = 0; j < M; ++j) {scanf(" %c", &pic[i][j]);wall += (pic[i][j] == 'X');if (pic[i][j] == 'S')start = { i, j };if (pic[i][j] == 'D')nd = { i, j };}if (wall + T >= M * N) {puts("NO");continue;} // 减枝pic[start.x][start.y] = 'X';if (DFS(start, 0))puts("YES");else puts("NO");}return 0;}




0 0