hdoj1010 Temperor of the bone

Tempter of the Bone

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


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
讲解:注意看清题的意思,题中说的是要,在正好的时间内,找到D,而不是小于或等于D
   特殊数据
6 6 37
S.....
......
......
......
......
D.....
NO
意思是在正好等于37 的时候 没有找到D
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<queue>
 6 int fangxiang[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
 7 const int MAX=101;
 8 char map[MAX][MAX];
 9 int mark[MAX][MAX];
10 int  n,m,t;
11 int start_x,start_y;
12 int end_x,end_y;
13 using namespace std;
14 bool DFS(int x,int y,int step)
15 {
16     int i,a,b;
17     if(map[x][y]=='D' && step==t)
18     return true;
19     if(x<1||x>n || y<1 || y>m)//判断到最后了,还没有找到
20     return false;
21     if(step>=t)//剪枝1:当step>=T时还没有找到D点
22     return false;
23     if(t-step<(abs(x-end_x)+abs(y-end_y)))//剪枝2:还需要的步数比理论上的最短距离还小
24     return false;
25     if((t-step-(abs(x-end_x)+abs(y-end_y)))%2!=0) //剪枝3:比理论上的最短距离多出来的必是偶数
26     return false;
27     for(i=0;i<4;i++)
28     {
29         a=x+fangxiang[i][0];
30         b=y+fangxiang[i][1];
31         if(a<=n && a>=1 && b>=1 && b<=m && map[a][b]!='X' && !mark[a][b]) //判断三个条件:1.检验_x,_y是否越界。2.看vis[][]是否访问过。3.看map[][]是否是墙
32         {
33             mark[a][b]=1;
34             if(DFS(a,b,step+1))
35             return true;
36             else
37             mark[a][b]=0;
38         }
39     }
40     return false;
41 }
42 int main()
43 {
44     int i,j;
45     while(cin>>n>>m>>t && n+m+t)
46     {
47         memset(mark,0,sizeof(mark));
48         for(i=1;i<=n;i++)
49         {
50             for(j=1;j<=m;j++)
51             {
52                  cin>>map[i][j];
53                  if(map[i][j]=='S')
54                  {
55                      start_x=i;
56                      start_y=j;
57                  }
58                  if(map[i][j]=='D')
59                  {
60                      end_x=i;
61                     end_y=j;
62                  }
63             }
64         }
65         mark[start_x][start_y]=1;
66         if(DFS(start_x,start_y,0))
67         cout<<"YES"<<endl;
68         else
69         cout<<"NO"<<endl;
70     }
71     return 0;
72 }
原文地址:https://www.cnblogs.com/lovychen/p/3227530.html