hdu1010(指定位置S到指定位置D走T步)

来源:互联网 发布:java分布式视频教程 编辑:程序博客网 时间:2024/05/01 05:45

Tempter(诱惑) of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 99840 Accepted Submission(s): 27043

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

题解:从S出发往深处dfs,标记此位置,当走到指定位置但是步数不等于t或者步数大于t时往回退一步,取消此位置的标记,然后继续往前dfs,一直到到达指定位置D并且步数等于t然后退出,需要剪枝,t不能小于S到D的最短距离,并且 当S到D的最短距离为偶数时,t必须为偶数,当S到D的最短距离为奇数时,t必须为奇数,因为无论如何绕,总会成偶数的增长。

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>using namespace std;char maze[7][7];int vis[7][7];int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};int n,m,flag,bx,by,t;void dfs(int x,int y,int step){    int i,mx,my;    if(x==bx&&y==by){        if(step==t)        flag=1;        return;    }    if(step>=t)return;    for(i=0;i<4;i++){        mx=x+dir[i][0];        my=y+dir[i][1];        if(maze[mx][my]!='X'&&mx>=0&&my>=0&&mx<n&&my<m&&!vis[mx][my]){            vis[mx][my]=1;            dfs(mx,my,step+1);            vis[mx][my]=0;            if(flag)return;        }       }}int main(){    int i,j,ax,ay;    while(scanf("%d%d%d",&n,&m,&t)!=EOF&&(n+m+t)){        for(i=0;i<n;i++){            getchar();            for(j=0;j<m;j++){                scanf("%c",&maze[i][j]);                if(maze[i][j]=='S'){                    ax=i;                    ay=j;                }                   if(maze[i][j]=='D'){                    bx=i;                    by=j;                }                       }                   }                   getchar();          if(abs(ax-bx)+abs(ay-by)>t||(int)(abs(ax-bx)+abs(ay-by)+t)%2!=0){            printf("NO\n");            continue;        }        memset(vis,0,sizeof(vis));        vis[ax][ay]=1;        flag=0;        dfs(ax,ay,0);        if(flag)printf("YES\n");        else printf("NO\n");    }     return 0;}
0 0