HDU 1010 Tempter of the Bone

题目:

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
题意描述:
输入矩阵的行列数N和M (1 < N, M < 7)以及出口打开的时间t
判断doggie能否在门打开时候正好到达门口。
解题思路:
搜索题,使用DFS搜索。题意很有意思,很容易理解错题意,认为在时间t内到达门口就可以,但是真正题意为必须正好在t秒的时候到达才行。较为容易实现的是DFS搜索,但是必须要剪枝,算是运用奇偶剪枝的典型题目。
在这里先说一下奇偶剪枝,我们知道在0-1矩阵中要从1走到0必须走奇数步,从0到1也是,但从1走到1,从0走到0都需要偶数步,总结一下就是必须相同才能用偶数步走到,否则都是奇数步,也即同奇同偶原则。
代码中tem的意义为剩余时间减去最短距离,而剩余时间和最短距离必须是同奇同偶的,所以tem的减数和被减数符合同奇同偶原则,那么tem作为差就必须是偶数才行,否则剪枝就可以了。
代码实现:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 char map[10][10];
 5 int n,m,t,book[10][10],ex,ey,flag;
 6 void dfs(int sx,int sy,int s);
 7 int main()
 8 {
 9     int i,j,sx,sy,x=0;
10     while(scanf("%d%d%d",&n,&m,&t),n+m+t != 0)
11     {
12         getchar();//吃掉空格 
13         for(x=0,i=1;i<=n;i++){
14             for(j=1;j<=m;j++){
15                 scanf("%c",&map[i][j]);
16                 if(map[i][j]=='S')
17                 {    sx=i;sy=j;    }
18                 if(map[i][j]=='D')
19                 {    ex=i;ey=j;    }
20                 if(map[i][j]=='X')
21                     x++;
22             }
23             getchar();//吃掉空格
24         }
25         
26         if(n*m - x -1< t)//剪枝方法1:路径剪枝 
27         {
28             printf("NO\n");
29             continue;
30         }
31         
32         flag=0;
33         memset(book,0,sizeof(book)); 
34         book[sx][sy]=1;
35         dfs(sx,sy,0);
36         if(flag)
37         printf("YES\n");
38         else
39         printf("NO\n");    
40     }
41     return 0;
42 }
43 void dfs(int x,int y,int s)
44 {
45     int tx,ty,k,tem;
46     int next[4][2]={1,0,0,-1,-1,0,0,1};
47     if(x==ex&&y==ey&&s==t)
48     {
49         flag=1;
50         return ;
51     }
52     tem=t-s-abs(ex-x)-abs(ey-y);
53     if(tem < 0 || tem%2==1)//剪枝方法2和3 ,ten小于零或者为奇数 
54         return;
55     for(k=0;k<=3;k++)
56     {
57         tx=x+next[k][0];
58         ty=y+next[k][1];
59         if(tx<1 || tx>n || ty<1 || ty>m || map[tx][ty]=='X')
60             continue;
61         if(!book[tx][ty])
62         {
63             book[tx][ty]=1;
64             dfs(tx,ty,s+1);
65             if(flag)
66                 return ;    
67             book[tx][ty]=0;
68         }
69     }
70     return; 
71 }

易错分析:

1、吃图的时候注意吃掉换行符

2、注意剪枝

posted @ 2017-08-01 13:17  Reqaw  阅读(1119)  评论(0编辑  收藏  举报