Tempter of the Bone--hdu1010--zoj2110

Tempter of the Bone

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

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

这个题主要是用深搜,看所有路径中有没有步数为给出的值的!

但是注意只用搜索一定会超时!!!

还要用 奇偶剪枝!

下面我讲一下奇偶剪枝

    

一起看这个图,要从S到E,如果没有障碍物#,最短的路径是6步!

但是有障碍物之后,我们就要绕路走,这时候的步数可以分为两部分  1.最短路径部分

                               2.走出最短路径的步数加上走回最短路径的步数(注意,走出去和走回的步数是相等的)

解释一下为什么会相等

看这个图,最短路径是黑色部分,现在要走红色部分,那么走出和走回最短路径的部分就是红色部分,为什么不算上蓝色的呢,因为如果把红色去掉,蓝色部分平移后就是最短路径!!

所以说,要想t步走到终点,多走的部分一定是偶数     即是(t-最短步数)一定为偶数,所以,我们就能剪枝了!

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 char map[8][8];
 6 int m,n,bx,by,ex,ey,step,t,flag;
 7 int mov[4][2]={0,1,0,-1,1,0,-1,0};
 8
 9 bool can(int x,int y)
10 {
11     if(x<0||x>m-1||y<0||y>n-1||map[x][y]==‘X‘)
12     return false;
13     return true;
14 }
15 void DFS(int x,int y)
16 {
17
18     int xx,yy,i;
19     if(x==ex&&y==ey)//判断是否找到终点
20     {
21         if(step==t)
22         flag=1;
23         return ;
24     }
25     if(step>t)
26         return ;
27        int dis=t-abs(ex-x)-abs(ey-y)-step;
28     if(dis<0||dis&1)//奇偶剪枝
29         return ;
30         if(flag==1)
31         return ;//当初我就是没加这一句,在hduoj上超时了,但是在zoj上能过
32     for(i=0;i<4;i++)
33     {
34         xx=x+mov[i][0];
35         yy=y+mov[i][1];
36         if(can(xx,yy))
37         {
38             step++;
39             map[xx][yy]=‘X‘;
40             DFS(xx,yy);
41             step--;
42             map[xx][yy]=‘.‘;
43         }
44     }
45 }
46 int main()
47 {
48     int i,j;
49     while(scanf("%d%d%d",&m,&n,&t),m||n||t)
50     {
51         getchar();//吸收回车符
52         for(i=0;i<m;i++)
53         {
54             for(j=0;j<n;j++)
55             {
56                 scanf("%c",&map[i][j]);
57                 if(map[i][j]==‘S‘)
58                 {
59                     bx=i;
60                     by=j;
61                 }
62                 if(map[i][j]==‘D‘)
63                 {
64                     ex=i;
65                     ey=j;
66                 }
67             }
68             getchar();
69         }
70         flag=0;
71         step=0;
72         int best=abs(ex-bx)+abs(ey-by);
73         if((best+t)&1)//首先判断一下,如果最短路径和要走的步数奇偶性不同,就直接输出NO
74         {
75              printf("NO\n");
76              continue;
77         }
78         map[bx][by]=‘X‘;
79         DFS(bx,by);//深搜
80         if(flag)
81         printf("YES\n");
82         else
83         printf("NO\n");
84     }
85     return 0;
86 }

下面看下修剪前后的时间差距

第三个是没剪枝的时候

第二个是没加满足情况就回溯那一句,在上面提到过

第一个是最后修剪成功

前两个在杭电都是超时的!

不懂得可以在下面提问,一定会尽快回复大家!谢谢

时间: 2024-11-29 10:34:06

Tempter of the Bone--hdu1010--zoj2110的相关文章

Tempter of the Bone —HDU1010(DFS+剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 102071    Accepted Submission(s): 27649 Problem Description The doggie found a bone in an ancient maze, which fascinated him

深搜--Tempter of the Bone hdu1010

这道题磨了我一个下午,虽然在大神帮助下完成,要注意的东西太多,之前还用了宽搜,那是每看懂题目意思,宽搜是找到一条最短路,而这道题不一定是最短路,要在规定的时间到达,换句话说就是有规定的步数,走了的不能再走. 写题中遇到以下几个问题: 1.对于深搜还是没有完全理解,由于是递归,所以每次的函数体应该都是一样的,之前把不该改变的起点步数清零放在递归函数里,导致错误. 2.对于我之前有点画蛇添足,我又加了个结构体,存迷宫每个点的横纵坐标和信息,然后我在递归结束条件那里直接判断的,但是我在赋值又没有对每个

HDU1010 Tempter of the Bone 【DFS】+【剪枝】

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 70895    Accepted Submission(s): 19535 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU1010 Tempter of the Bone(小狗是否能逃生----DFS)

Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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 doggi

ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)

题意  一仅仅狗要逃离迷宫  能够往上下左右4个方向走  每走一步耗时1s  每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次  问狗是否有可能逃离这个迷宫 直接DFS  直道找到满足条件的路径  或者走全然部可能路径都不满足 注意剪枝  当前位置为(r,c)  终点为(ex,ey) 剩下的时间为lt  当前点到终点的直接距离为  d=(ex-r)+(ey-c)   若多走的时间rt=lt-d<0 或为奇数时  肯定是不可能的  能够自己在纸上画一下 每一个点仅仅能走一次的图  走弯路的话多

hdu1010 Tempter of the Bone(DFS+剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 90716    Accepted Submission(s): 24683 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

【HDU - 1010】Tempter of the Bone(dfs+剪枝)

Tempter of the Bone 直接上中文了 Descriptions: 暑假的时候,小明和朋友去迷宫中寻宝.然而,当他拿到宝贝时,迷宫开始剧烈震动,他感到地面正在下沉,他们意识到这是一个陷阱!他们想尽一切办法逃出去.迷宫是一个大小为 N*M 的长方形,迷宫中有一扇门.一开始,门是关着的,他会在第 t 秒的时间打开.因为,小明和朋友必须在第 t 秒到大门口.每一秒,他都可以向上下左右四个方向移动一个点.一旦他移动了,他刚才所在的点就消失,(这意味着他不能回到他已经走过的点).他不能在一个

Tempter of the Bone

Tempter of the Bone Time Limit: 1000ms                                            Memory Limit: 32768KB 64-bit integer IO format:      Java class name: The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it

Tempter of the Bone(dfs奇偶剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 92175    Accepted Submission(s): 25051 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU - 1010 Tempter of the Bone 深搜模板题(DPS)解题报告

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 88587    Accepted Submission(s): 24116 Problem Description The doggie found a bone in an ancient maze, which fascinated him a