HDU 1010 Tempter of the Bone (DFS)

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 57517    Accepted Submission(s): 15539


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 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 would 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 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 at 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 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 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

这题太恶心了,在写DFS的时候,如果操作步骤稍微多一点就会TLE

在这题学会了剪枝还有写dfs的时候一定要尽量减少自己的步数

另外,这题如果用单个字符输入的话,注意在最后一个换行的时候getchar

代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<limits.h>
using namespace std;
int ans= 0;
int T;
int map[10][10];
int hash[10][10];
int a2,b2;
int Fabs(int x)
{
    return x>0? x: -x;
}
void dfs(int x,int y,int step)
{
    if(ans)
        return ;
    if(map[x][y]==2&&step==T)
    {
        ans= 1;
        return;
    }
    else if(Fabs(a2-x)+ Fabs(b2- y)> T-step)
            return;
    else
    {        
        int xx[]={0,1,0,-1,0};
        int yy[]={0,0,-1,0,1};
        int a,b,s;
        for(int i=1; i<= 4; i++)
        {
            s= step+1;
            a= x+xx[i];
            b= y+yy[i];
            if(map[a][b]==1&&hash[a][b]==0)
            {    
                hash[a][b]= 1;
                //printf("%d %d %d %d %d\n",x,y,a,b,s);
                dfs(a,b,s);
                hash[a][b]= 0;
            }
            else if(map[a][b]==2&&s==T)
            {
                hash[a][b]= 1;
                //printf("%d %d %d %d %d\n",x,y,a,b,s);
                dfs(a,b,s);
                hash[a][b]= 0;
            }
        }
    }
}
int main()
{
    int m,n;
    while(scanf("%d%d%d",&m,&n,&T)!=EOF&&m+n+T)
    {
        char ch[10];
        ans= 0;
        memset(map,0,sizeof(map));
        memset(hash,0,sizeof(hash));
        int x,y;
        for(int i=1; i<= m; i++)
        {
            scanf("%s",&ch);
            for(int j=0; j< n; j++)
            {
                if(ch[j]=='X')
                    map[i][j+1]= 0;
                else
                {
                    map[i][j+1]= 1;
                    if(ch[j]=='S')
                    {
                        x= i;
                        y= j+1;
                    }
                    else if(ch[j]=='D')
                        {
                            map[i][j+1]= 2;
                            a2= i;
                            b2= j+1;
                        }
                }    
            }
        }
        int t= Fabs(a2-x)+ Fabs(b2- y);
        if(t > T||T> m*n-1)
            printf("NO\n");
        else
        {
            t= T- t;
            if(t%2==0)
            {
                hash[x][y]= 1;
                dfs(x,y,0);
                if(ans)
                    printf("YES\n");
                else
                    printf("NO\n");
            }
            else
                printf("NO\n");
        }
    }
    return 0;
}


 

 

 

你可能感兴趣的:(搜索,HDU)