HDU1010 Tempter of the Bone

Source: Internet
Author: User

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

This is a basic DFS search topic that walks through each possible path to see if you can get out of the maze. Attention here is pruning, first of all, the number of squares can be less than the specified time out of the maze, it is certainly impossible to get out of the maze, and secondly, is the odd-even nature pruning. This is the first DFS topic I've done. It is also important to note that once a path is found, stop the search immediately, otherwise it will time out. The AC code is as follows:

#include <cstdio>#include <iostream>#include <cstdlib>using namespace STD;Char Map[9][9];intN,M,T,DI,DJ;intdir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//Four way to save in an arrayBOOLEscape=0;//Record whether to get out of the mazevoidDfsintSiintSjintCNT) {intI,temp;if(si>n| | sj>m| | si<=0|| sj<=0)//See if out of bounds        return;if(CNT==T&AMP;&AMP;SI==DI&AMP;&AMP;SJ==DJ)//See if the Escape{escape=true;return; } temp= (T-CNT)-ABS(Si-di)-ABS(SJ-DJ);//odd-even pruning    if(temp<0|| temp&1)//When the minimum number of steps for the current position to exit is greater than the remaining time, or if their difference is odd (parity pruning), it directly indicates that the path is not going through        return; for(intI=0;i<4; i++)//Traverse all paths{if(Map[si+dir[i][0]][sj+dir[i][1]]!=' X ')        {Map[si+dir[i][0]][sj+dir[i][1]]=' X ';//Avoid duplicationDFS (si+dir[i][0],sj+dir[i][1],cnt+1);Map[si+dir[i][0]][sj+dir[i][1]]='. ';//Restore Initial state            if(Escape)return;//Here is the key, if found, no need to find, otherwise it will time out}    }return;}intMain () {intSI,SJ; while(Cin>> n >> m >> t) {if(n==0&&m==0&&t==0) Break;intWall=0; for(intI=1; i<=n;i++) { for(intj=1; j<=m;j++) {Cin>>MapI [j];if(MapI [j]==' S ') {si=i;                Sj=j; }Else{if(MapI [j]==' D ') {di=i;                    Dj=j; }Else{if(MapI [j]==' X ') wall++; }                }            }        }if(n*m-wall<=t)//Note the equals sign here{cout<<"NO"<< Endl;Continue; } escape=false;Map[Si] [sj]=' X '; DFS (SI,SJ,0);if(Escape)cout<<"YES"<< Endl;Else            cout<<"NO"<< Endl; }return 0;}

HDU1010 Tempter of the Bone

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.