Zoj 2110 tempter of the bone (conditional maze DFS, hdu1010)

Source: Internet
Author: User

A dog needs to escape from the maze. It can go up, down, left, right, and left. Every step takes 1 S. Each grid can only go once. The door of the Maze can only be opened once at the T moment. escape from this maze

Direct DFS direct path to locate the path that meets the conditions or finish all possible paths are not met

Note that the current position of pruning is (R, C) and the end point is (ex, ey). The remaining time is <direct distance from the current point to the end point is d = (ex-R) + (ey-C) if the RT = lt-D <0 or an odd number is used, it is impossible to draw a picture on the paper. If the detour can only be performed once at each point, the number of multiple steps must be an even number.

#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)  //满足条件或已经满足条件    {        ans = true;        return;    }    char tc=mat[r][c];  //保存原来的可能值 有'D'和'.'两种情况    mat[r][c] = 'X';    int rt = lt - abs(ex - r) - abs(ey - c);  //比直线到达终点多用的时间    if(rt >= 0 && rt % 2 == 0)   //剪枝    for(int i = 0; i < 4; ++i)   //4个方向走    {        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;  //恢复原状}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 existing 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 do Or 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 disappe Ar in the next second. He cocould not stay at one block for more than one second, nor cocould 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



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.