ZOJ 2110 Tempter of the Bone (conditional Maze dfs,hdu1010)

Source: Internet
Author: User

Test instructions a dog to escape from the maze can go up and down in 4 directions each step takes 1s each of the lattice can only walk once and the maze of the door only at t time Open once asked the dog whether it is possible to escape the maze

Direct DFS straights find a path that satisfies a condition or the total may not be satisfied.

Note that the current position is (R,C) at the end of (Ex,ey) the remaining time is the current point to the end of the direct distance of d= (ex-r) + (EY-C) if the time to go rt=lt-d<0 or an odd number of times is certainly not possible to draw on the paper each point only Can only walk once the figure detours, the number of steps must be even

#include <cstdio> #include <cmath>using namespace Std;int dx[4] = {0, 0,-1, 1};int dy[4] = {-1, 1, 0, 0};const  int N = 10;char mat[n][n];bool ans;int T, SX, SY, EX, ey;void dfs (int r, int c, int lt) {if (mat[r][c] = = ' D ' && lt = = 0| |        ANS)//satisfies the condition or already satisfies the condition {ans = true;    Return  } Char Tc=mat[r][c]; Saving the original possible values are ' D ' and '. '    Two cases mat[r][c] = ' X ';  int rt = Lt-abs (EX-R)-ABS (EY-C); More time than the line reached the end of the if (RT >= 0 && rt% 2 = = 0)//pruning for (int i = 0; i < 4; ++i)//4 directions Walk {int        x = r + dx[i], y = c + dy[i];    if (mat[x][y] = = '. ' | | mat[x][y] = = ' D ') Dfs (x, y, lt-1);  } Mat[r][c] = TC;    Restitution}int Main () {int n, m;  while (scanf ("%d%d%d", &n, &m, &t), N) {for (int i = 1; I <= n; ++i) scanf ("%s", Mat[i]        + 1); for (int i = 1, i <= N; ++i) for (int j = 1; j <= m; ++j) {if (mat[i][j] = = ' S ')       SX = i, sy = j;         if (mat[i][j] = = ' D ') ex = i, ey = j;        } ans = false;        DFS (SX, SY, T); printf (ans?)    "yes\n": "no\n"); } return 0;}

Tempter of the Bone Time limit: 2 Seconds Memory Limit: 65536 KB

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



ZOJ 2110 Tempter of the Bone (conditional Maze dfs,hdu1010)

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.