HDU 1010 Tempter of the Bone(DFS+暴力+标记)

来源:互联网 发布:js 拖动事件 编辑:程序博客网 时间:2024/06/05 17:25

Tempter of the Bone

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


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
 

Author
ZHANG, Zheng
 

Source
ZJCPC2004
 

Recommend
JGShining  &nbsp|  &nbspWe have carefully selected several similar problems for you:  1016 1241 1242 1072 1312 
 

Statistic | Submit | Discuss | Note

题意

给你一个n*m的图,S表示起点,D表示终点,X表示墙,是不能走的。再给你一个T,问你能不能在走了正好T步之后,从S开始走正好可以达到D,每个点只能走一次。

思路

这题其实暴力DFS可以过..对于现在走到的点,考虑上下左右四个方向,在走这一步前打个标记,然后dfs传递下去,在dfs完之后,就将这个标记消除。如果走到X,那么直接return,最后当走到D这个点时,判断一下走的步数是不是等于T就行了。

Code

#pragma GCC optimize(3)#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>#include<cctype>#include<climits>#include<cstdlib>#include<cmath>#include<queue>#include<stack>#include<climits>#include<vector>#include<bitset>using namespace std;typedef long long ll;inline void readInt(int &x) {    x=0;int f=1;char ch=getchar();    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();    x*=f;}inline void readLong(ll &x) {    x=0;int f=1;char ch=getchar();    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();    x*=f;}/*================Header Template==============*/char mp[10][10];int n,m,t,sx,sy,ex,ey;bool vis[10][10],use[10][10],f;int step[2][4]={{0,1,0,-1},{1,0,-1,0}};inline void dfs(int x,int y,int w) {//  cout<<x<<" "<<y<<" "<<w<<endl;    if(x==ex&&y==ey) {        if(w==t)            f=1;        return;    }    if(vis[x][y]||(((abs(x-ex)+abs(y-ey))&1)!=((t-w)&1))||w>=t)        return;    for(int i=0;i<4;i++) {        int px=step[0][i],py=step[1][i],nx=x+px,ny=y+py;        if(nx<1||nx>m||ny<1||ny>n||use[nx][ny])            continue;        use[nx][ny]=1;        dfs(nx,ny,w+1);        use[nx][ny]=0;        if(f)            break;    }    return;}int main() {    while(1) {        readInt(n);        readInt(m);        readInt(t);        memset(vis,0,sizeof vis);        memset(use,0,sizeof use);        if(!n&&!m&&!t)            break;        for(int i=1;i<=n;i++) {            scanf("%s",mp[i]+1);            for(int j=1;j<=m;j++) {                if(mp[i][j]=='S') {                    sx=j;                    sy=i;                }                if(mp[i][j]=='D') {                    ex=j;                    ey=i;                }                if(mp[i][j]=='X')                    vis[j][i]=1;            }        }        f=0;        use[sx][sy]=1;        dfs(sx,sy,0);        puts(f?"YES":"NO");    }}