HDU 1010 Tempter of the Bone

来源:互联网 发布:爱钱进可靠吗 知乎 编辑:程序博客网 时间:2024/05/16 17:46
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 .
. . X D
. . . .
3 4 5
S . X .
. . X .
. . . D
0 0 0
 

Sample Output
NO
YES

    开始做的时候没有读题,直接看的数据,以为在t时间以内到门就可以。所以果断用BFS,写完测试了1个小时怎么也过不来,才明白是要在时间t到达门口,转而用DFS带走。先上代码

#include<iostream>#include<string.h>using namespace std;int Hash[6][6];//一个检测是否走过的数组int sum,ei,ej,step,m,n,flag;//flag用于标记是否能够出去。void dfs(int i,int j,int sum){    int c;    if((ei==i && ej==j )|| sum>=step)    {        if(sum==step && ei==i &&ej==j)flag=1;        return;    }    else    {        Hash[i][j]=1;        if(i-1>=0 && Hash[i-1][j]==0)dfs(i-1,j,sum+1);        if(i+1<m && Hash[i+1][j]==0)dfs(i+1,j,sum+1);        if(j-1>=0 && Hash[i][j-1]==0)dfs(i,j-1,sum+1);        if(j+1<n && Hash[i][j+1]==0)dfs(i,j+1,sum+1);    //4个方向。        Hash[i][j]=0;//注意回溯的时候要把标记清除。    }    return;}int main(){    char maze[6][6];    int si,sj,i,j;    while(cin>>m>>n>>step)    {        if(m==0 && n==0 && step==0)break;        memset(Hash,0,sizeof(Hash));        for(i=0;i<m;i++)            for(j=0;j<n;j++)            {                cin>>maze[i][j];                if(maze[i][j]=='S')                {                    si=i;                    sj=j;                }                if(maze[i][j]=='D')                {                    ei=i;                    ej=j;                }                if(maze[i][j]=='X')                {                    Hash[i][j]=1;//如果走不了,就标记为1.                }            }        if((si+sj+ei+ej+step)%2==1)cout<<"NO"<<endl;//很精巧的剪枝,我来举个例子/*6 6 36S . . . . . .. . . . . . .. . . . . . .. . . . . . .. . . . . . .D . . . . . .从这组数据来看 无论怎么走 S到D 的时间都是奇数,而给定的t是偶数,所以直接NO;可以看出来 时间t%2==((si-ei)+(sj-ej))%2 从而得到这个公式。*/        else        {            flag=0;            dfs(si,sj,0);            if(flag==1)                cout<<"YES"<<endl;            else if(flag==0)                cout<<"NO"<<endl;        }    }    return 0;}


PS:刚开始的时候直接声明一个hash,但是在hdoj上有二义性。以后注意。
0 0
原创粉丝点击