HDU 1010 Tempter of the Bone

来源:互联网 发布:js实现隐藏显示 编辑:程序博客网 时间:2024/05/12 20:45

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
题解:本体的大概意思是给你一个N*M的矩阵迷宫,S代表起点,D代表终点,X代表障碍。你到达终点的时间必须与T相同。我用的是深搜来解决这个问题。但是要注意必须剪枝,否则会超时。因为你必须在T时刻到达终点,所以若你到达终点的时间不是T,并且与T的差值不是偶数,就应该舍去。(因为要能够在T时刻到达终点的话,就必须在到达D后拥有偶数倍的时间来来回移动。)还有就是如果走完所有点还没有到达T时刻也应该舍弃.

#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>typedef struct s{    int x;    int y;    int t;    int di; }p;int main(){    p mo[1000];    int map[10][10];    int a[10][10];    int N,M,T,blog;    int pop=-1;    int d[4][2]={{0,1},{0,-1},{1,0},{-1,0}};    while(scanf("%d %d %d",&N,&M,&T)!=EOF)    {        for(int i=0;i<1000;i++)            mo[i].t=0;            blog=0;            pop=-1;        for(int i=0;i<10;i++)        for(int j=0;j<10;j++)            a[i][j]=0;        if(N==0&&M==0&&T==0)            break;            getchar();        for(int i=1;i<=N;i++)        {            for(int j=1;j<=M;j++)            scanf("%c",&map[i][j]);        getchar();        }        for(int i=1;i<=N;i++)        for(int j=1;j<=M;j++)        {            if(map[i][j]=='S')            {            pop++;            mo[pop].x=i;            mo[pop].y=j;            mo[pop].di=0;            mo[pop].t=0;            a[i][j]=1;            }        }        if(N*M<T)        {            printf("NO\n");            continue;        }        int x,y,di,t;        while(pop>=0)        {            x=mo[pop].x;            y=mo[pop].y;            t=mo[pop].t;            di=mo[pop].di;            if(map[x][y]=='D'&&(int)fabs(T-t)%2==1)               break;            if(map[x][y]=='D'&&t==T)            {                blog=1;                break;            }            while(di<4)            {                if((x+d[di][0])>=1&&(x+d[di][0])<=N&&(y+d[di][1])>=1&&(y+d[di][1])<=M&&map[x+d[di][0]][y+d[di][1]]!='X'&&a[x+d[di][0]][y+d[di][1]]==0)                break;                di++;               }            if(di>=4)            {            a[x][y]=0;            pop--;            }            else            {            mo[pop].di=di+1;            pop++;            mo[pop].x=x+d[di][0];            mo[pop].y=y+d[di][1];            mo[pop].di=0;            mo[pop].t=t+1;            a[x+d[di][0]][y+d[di][1]]=1;            }        }        if(blog==1)        printf("YES\n");        else        printf("NO\n");    }    return 0;}
0 0