HDU1010 Tempter of the Bone

题目链接

Tempter of the Bone

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


 

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

典型的DFS,剪枝是关键。这题有个超强剪枝--奇偶剪枝。还有一些其他剪枝。

戳此了解曼哈顿距离

戳此了解奇偶剪枝

AC代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<iomanip>
 5 #include<vector>
 6 #include<cmath>
 7 #include<stack>
 8 using namespace std;
 9 char maze[10][10];//迷宫 
10 int N,M,T;
11 int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};//上下左右四个方向 
12 bool flag=0;//是否能出去 
13 bool visited[10][10];//走过
14 int ex,ey;//终点坐标 
15 void dfs(int i,int j,int t)//第t秒到达迷宫的i行j列 
16 {
17     if(maze[i][j]=='D') 
18     {   
19         if(t==T) flag=1;
20         return; 
21     }
22     if(t==T) return;//时间剪枝 
23     int d=abs(i-ex)+abs(j-ey);//曼哈顿距离
24     if(T-t<d) return;//曼哈顿距离剪枝 
25     if((T-t-d)&1) return;//奇偶性剪枝 (T-t-d)&1==1就是(T-t-d)%2==1的意思 
26         visited[i][j]=true;
27     
28     for(int p=0;p<4&&!flag;p++)
29     {
30         int x=i+dx[p],y=j+dy[p];
31         if(i<1||i>N||j<1||j>M) continue;//不能超出迷宫边界
32         if(maze[x][y]=='X') continue;//wall
33         if(visited[x][y]) continue;//已经走过  
34         
35         dfs(x,y,t+1);
36         if(visited[x][y]) visited[x][y]=false;
37     }
38 }
39 int main()
40 {
41     int sx,sy;//起点坐标 
42     while(cin>>N>>M>>T,N!=0&&M!=0&T!=0)
43     {
44     getchar();
45     memset(visited,0,sizeof(visited));
46     for(int i=1;i<=N;i++)
47     {
48         for(int j=1;j<=M;j++)
49         {
50             maze[i][j]=getchar();
51             if(maze[i][j]=='S') sx=i,sy=j;
52             if(maze[i][j]=='D') ex=i,ey=j;
53         }
54         getchar();
55     }
56     dfs(sx,sy,0);
57     
58     if(flag) cout<<"YES"<<endl;
59     else cout<<"NO"<<endl;
60     
61     flag=0;
62         }
63 }
64 //这题貌似还有个剪枝,令wall=X的数量,当n*m-wall<=t时也不能到达终点 
View Code
原文地址:https://www.cnblogs.com/wangzhebufangqi/p/12796179.html