HDU 1010 & amp; ZOJ 2110 Tempter of the bone (DFS + parity pruning), hdu1010

Source: Internet
Author: User

HDU 1010 & ZOJ 2110 Tempter of the bone (DFS + parity pruning), hdu1010
Problem Description:

The doggie found a bone in an internal ent maze, which fascinated him a lot. however, when he picked it up, the maze began to shake, and the doggie cocould 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 wocould 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 cocould move one block to one of the upper, lower, left and right neighboring blocks. once he entered a block, The ground of this block wocould start to sink and disappear in the next second. he coshould not stay at one block for more than one second, nor coshould he move into a visited block. can the poor doggie keep ve? 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 have ve, 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 maxn100 # 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; // whether to escape int Sx, Sy, Ex, Ey, wall; // start coordinate, target coordinate, number of walls const int dx [] = {0, 0, 1,-1}; // direction array const int dy [] = {-1, 1, 0, 0 }; bo Ol check (int x, int y) // determine whether to move toward the next grid {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) {// escape = true; return;} // abs (x-ex) + abs (y-ey) the distance from the current grid to the target grid (no diagonal line can be taken) // T-cnt is the actual number of remaining steps, do them poorly // If tmp <0 or tmp is an odd number, it cannot be reached! Int tmp = (T-cnt)-fabs (px-Ex)-fabs (py-Ey ); // if (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 to the wall DFS (xx, yy, cnt + 1); // perform in-depth traversal if (escape) return; Map [xx] [yy] = '. '; // status recovery, backtracking} return;} int main (int argc, char * argv []) {while (~ Scanf ("% 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 ++; // count the number of blocks on the wall} if (N * M-wall <= T) {// pruning puts ("NO"); continue;} escape = false; // The value is initialized to false Map [Sx] [Sy] = 'X '; // The passing block is converted into a wall to ensure that the DFS (Sx, Sy, 0) is passed at a time; if (escape) puts ("YES"); else puts ("NO ");} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.