Tempter of the Bone HDU

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. 

InputThe 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. 
OutputFor 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,但第一次提交超时,说明需要剪枝,通过路径剪枝,奇偶剪枝,通过。
AC Code:
#include<iostream>
#include<utility>
#include<cmath>
using namespace std;
typedef pair<int,int> P;
int INF=0x3f3f3f3f;
int N,M,T,sx,sy,gx,gy,flag,f;
char maze[8][8];
int vis[8][8];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
void dfs(P p){
    if(p.first ==gx&&p.second ==gy&&vis[p.first][p.second]==T){
        flag=1;
        return;
    }
    if(vis[p.first ][p.second]>=T) return;//当前的步数超过要求的步数,退出 
    int mins=T-vis[p.first ][p.second]-abs(p.first-gx)-abs(p.second-gy);//最短距离 
    if(mins%2||mins<0) return;  // 剩余步数小于最短距离或者满足奇偶剪枝条件
        for(int i=0;i<4;i++){
            int nx=p.first +dx[i],ny=p.second +dy[i];
            if(nx>=0&&nx<N&&ny>=0&&ny<M&&maze[nx][ny]!='X'&&vis[nx][ny]==INF){
                vis[nx][ny]=vis[p.first][p.second]+1;
                dfs(P(nx,ny));
                vis[nx][ny]=INF;
            }
        }
    
}
int main(){
    while(~scanf("%d%d%d",&N,&M,&T),N+M+T){
        int holdback=0;flag=0;
        for(int i=0;i<N;i++)
            for(int j=0;j<M;j++){
                cin>>maze[i][j];
                vis[i][j]=INF;
                if(maze[i][j]=='S') sx=i,sy=j,vis[sx][sy]=0;
                if(maze[i][j]=='D') gx=i,gy=j;
                if(maze[i][j]=='X') holdback++;//记录阻碍 X 
            }
        if(N*M-holdback>T) dfs(P(sx,sy));//可通行的点必须大于要求的步数  路径剪枝 
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    
    }
}
原文地址:https://www.cnblogs.com/astonc/p/9901320.html