HDU-1010 Tempter of the Bone deep search template question (DPS) solution report

Source: Internet
Author: User

HDU-1010 Tempter of the Bone deep search template question (DPS) solution report

 

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 88587 Accepted Submission (s): 24116



Problem Description The doggie found a bone in an alert 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 5S.X...X...XD....3 4 5S.X...X....D0 0 0

Sample Output
NOYES

Author ZHANG, Zheng:

 

Here is a maze of N rows and M columns. X represents the wall, a path, S represents the start point, and D represents the end point. Ask if the end point can be reached from the start point in K seconds.

Question:

Template DFS, No pruning is required. Open a two-dimensional array to show up and down, and open an oount to record the number of steps. If count> K, you can directly break it before it reaches.

Reference code:

 

#include
 
  #include
  
   using namespace std;char str[10][10];int map[10][10],v[4][2]={0,1,0,-1,1,0,-1,0};int n,m,k,i,j,flag,ax,ay,bx,by;void dfs(int x,int y,int count){    int mx,my;    if(bx==x&&by==y)    {        if(k==count)            flag=1;        return;    }    if(count>=k)        return;    if(str[x][y]!='X')    {        for(int i=0;i<4;i++)        {            mx=x+v[i][0];            my=y+v[i][1];            if(mx>=1&&mx<=n&&my>=1&&my<=m&&!map[mx][my]&&str[mx][my]!='X')            {                map[mx][my]=1;                dfs(mx,my,count+1);                map[mx][my]=0;                if(flag)                   return;            }        }    }}int main(){        while(scanf(%d%d%d,&n,&m,&k)!=EOF&&(n+m+k))    {        memset(map,0,sizeof(map));        int count;        for(i=1;i<=n;i++)        {            getchar();            for(j=1;j<=m;j++)            {                scanf(%c,&str[i][j]);                if(str[i][j]=='S')                {                    ax=i;                    ay=j;                }                if(str[i][j]=='D')                {                    bx=i;                    by=j;                }            }        }        getchar();        if(abs(bx-ax)+abs(by-ay)>k||(ax+bx+ay+by+k)%2==1)            printf(NO);        else         {            flag=0;            count=0;            map[ax][ay]=1;            dfs(ax,ay,count);            if(flag)                printf(YES);            else                 printf(NO);        }    }    return 0;} 
  
 


 

 

 

Related Article

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.