HDU--1010 Tempter of the Bone(深搜+奇偶剪枝)

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1010

我认为的剪枝就是在本来的代码中加入一些附加条件使之不去进行多余的计算,防止超时

奇偶剪枝的知识链接

AC代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 int n,m,t,k,flag,starex,starey,endx,endy,sum;
 6 char s[10][10];
 7 int book[10][10];
 8 int tx,ty,head,tail;
 9 struct node
10 {
11     int x,y,f;
12 }que[500];
13 int abs(int a)
14 {
15     if(a<0)
16     a=-a;
17     return a;
18 }
19 int dfs(int x,int y,int step)
20 {
21     int tx,ty;
22     int a[4]={1,-1,0,0},b[4]={0,0,1,-1};
23     if(flag==1)
24     return 0;
25     if(t==step&&x==endx&&y==endy)
26     {
27         flag=1;
28         return 0;
29     }
30     int mindis=abs(x-endx)+abs(y-endy);  /*当前点到终点的最短距离*/
31     if(mindis>t-step||(mindis+ t-step)%2!=0)
32     return 0;
33     for(int i=0;i<4;i++)
34     {
35         tx=x+a[i];
36         ty=y+b[i];
37         if(tx>=n||tx<0||ty>=m||ty<0)
38         continue;
39         if((s[tx][ty]==‘.‘||s[tx][ty]==‘D‘)&&book[tx][ty]==0)
40         {
41             book[tx][ty]=1;
42             dfs(tx,ty,step+1);
43             book[tx][ty]=0;
44         }
45     }
46     return 0;
47 }
48 int main()
49 {
50     while(~scanf("%d%d%d",&n,&m,&t))
51     {
52         sum=0;
53         if(n==0&&m==0&&t==0)
54         break;
55         for(int i=0;i<n;i++)
56         {
57             scanf("%s",s[i]);
58             for(int j=0;j<m;j++)
59             {
60                 if(s[i][j]==‘S‘)
61                 {
62                     starex=i;starey=j;
63                 }
64                 if(s[i][j]==‘D‘)
65                 {
66                     endx=i;endy=j;
67                 }
68                 if(s[i][j]==‘X‘)
69                 {
70                     sum++;
71                 }
72             }
73         }
74         if(n*m-sum-1<t)
75         {
76             printf("NO\n");
77             continue;
78         }
79         flag=0;
80         dfs(starex,starey,0);
81         if(flag==1)
82         printf("YES\n");
83         else
84         printf("NO\n");
85     }
86     return 0;
87 }

时间: 2024-08-14 03:51:39

HDU--1010 Tempter of the Bone(深搜+奇偶剪枝)的相关文章

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

HDU 1010 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): 86037    Accepted Submission(s): 23462 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

hdu 1010 Tempter of the Bone 深搜+剪枝

题意:在一个坐标内,给定起点和终点,问能否恰好在t时刻到达终点. 以前很少写搜索题,所以看到这个题,就按照普通的深搜写了一下,交上去超时了.后来在网上搜了一下才知道,要剪枝才行.可是,我以前从没写过剪枝,不知道怎么剪,就按照别人的思路往下想.看懂以后,我对剪枝的理解是:对于一些没有必要继续搜索的路径,不再往下深搜,提前返回到上一层.花了半天时间调试代码,终于AC了. 奇偶剪枝:根据题目,doggie必须在第t秒到达门口.也就是需要走t-1步.设doggie开始的位置为(sx,sy),目标位置为(

HDU 1010 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): 70665    Accepted Submission(s): 19487 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Temper of the bone(深搜+剪枝)

Tempter of the Bone 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

HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)

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 despe

【深搜加剪枝五】HDU 1010 Tempter of the Bone

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

HDU 1010 Tempter of the Bone heuristic 剪枝法

本题就是考剪枝法了. 应该说是比较高级的应用了.因为要使用heuristic(经验)剪枝法.要总结出这个经验规律来,不容易.我说这是高级的应用也因为网上太多解题报告都没有分析好这题,给出的程序也很慢,仅仅能过掉,由此看来很多人没有做好这道题. 这里我需要更正一下网上流行的说法:奇偶剪枝法. 其实本题使用奇偶剪枝法并不能太大提高速度,只能说仅仅让使用奇偶剪枝过掉.所以网上说本题使用奇偶剪枝的,其实并不能提高速度. 原因: 奇偶剪枝只能剪枝一次,不能在递归的时候剪枝,因为只要初始化位置符合奇偶性,那

HDU 1010 Tempter of the Bone dfs+剪枝

给你一个迷宫一个起点和一个终点,问你能否走T步刚好到达终点,不能重复走,并且只有4个方向 显然这是一个dfs,虽然N最大只有7,但是裸的dfs复杂度还是太高了,因此要进行一些剪枝 1.如果T比图上所有的可走点还要大,肯定是不可行的.这个可以避免dfs整张图. 2.奇偶剪枝,有性质当前点(x,y)到目标点(tx,ty)的所有路径的长度的奇偶性一定和|x-tx|+|y-ty|一样. #include <cstdio> #include <iostream> #include <c

hdu 1010 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): 68206    Accepted Submission(s): 18719 Problem Description The doggie found a bone in an ancient maze, which fascinated him a