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 is a trap, and he tried desperately to get out of this maze.
The maze is a rectangle with sizes N by M. There is a door in the maze. At the beginning, the door is 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 in 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 on 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 all test case contains three integers n, m, and T (1 < N, m < 7; 0 < T <), which denote The sizes of the maze and the time at which the door would open, respectively. The next N lines give the maze layout, with all 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 is a 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
Detailed code and analysis are as follows:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath > #include <map> #include <set> #include <algorithm> #define MAXN 100#define RST (n) memset (n, 0, sizeof (N)) using namespace Std;char MAP[MAXN][MAXN]; Map int N, M, T; Map size, escape time bool escape = false; can escape int Sx, Sy, Ex, Ey, wall; Starting coordinates, target coordinates, number of walls const int dx[] = {0, 0, 1,-1}; Direction Array const int dy[] = {-1, 1, 0, 0};bool Check (int x, int y)//Determine if you can move down a lattice {return x>=0 && y>=0 & ;& x<n && y<m && map[x][y]!= ' x ';} void DFS (int px, int py, int cnt) {if (Px==ex && py==ey && cnt==t) {//smooth escape escape = true; return; }//abs (X-ex) +abs (Y-ey) indicates that the distance from the grid to the target lattice is now (cannot walk diagonally)//t-cnt is the actual number of steps required, do not make them worse//if TMP<0 or TMP is odd, it is unreachable! int tmp = (t-cnt)-Fabs (Px-ex)-fabs (Py-ey); Parity pruning if during the search (tmp < 0 | | tmp% 2) return; for (int i=0; i<4; i++) { int xx = px + dx[i]; int yy = py + dy[i]; if (check (xx, yy)) {Map[xx][yy] = ' X '; Set the current square as a wall DFS (xx, yy, cnt+1); Depth traversal if (escape) return; MAP[XX][YY] = '. '; State recovery, backtracking}} return; int main (int argc, char *argv[]) {while (~SCANF ("%d%d", &n, &m, &t) && N | | M | | T) {wall = 0; for (int i=0; i<n; i++) {//Map input scanf ("%s", Map[i]); for (int j=0; MAP[I][J]; J + +) {if (map[i][j] = = ' S ') {sx=i, sy=j;} else if (map[i][j] = = ' D ') {ex=i, ey=j;} else if (map[i][j] = = ' X ') wall++; Number of blocks of statistics wall} if (N*m-wall <= T) {//Pre-Search pruning puts ("NO"); Continue } escape = false; Initialize to false Map[sx][sy] = ' X '; Walk through the block into a wall, guaranteed once through DFS (Sx, Sy, 0); if (escape) puts ("YES"); Else puts ("NO"); } return 0;}
HDU 1010 && ZOJ 2110 Tempter of the bone (DFS + parity pruning)