Tempter of the Bone HDU - 1010

Tempter of the Bone HDU - 1010

dfs。

几个剪枝:

1.如果当前所用时间加上当前位置到目标的曼哈顿距离之和大于目标时间,那么显然无论如何不能完成。剪掉

2.在搜索前判一下,如果出发位置到目标的曼哈顿距离与目标时间的奇偶性不同,那么显然无论如何不能完成。剪掉

错误原因:未加第二个剪枝,TLE

 1 #include<cstdio>
 2 char s[10][10];
 3 bool ok;
 4 int n,m,t,tx,ty,sx,sy;
 5 int abs(int x)
 6 {
 7     return x>0?x:-x;
 8 }
 9 void dfs(int x,int y,int num)
10 {
11     if(num>t)    return;
12     if(abs(tx-x)+abs(ty-y)+num>t)    return;
13     if(x==tx&&y==ty)
14     {
15         if(num==t)
16         {
17             puts("YES");
18             ok=1;
19         }
20         return;
21     }
22     s[x][y]=‘X‘;
23     if(x>1&&s[x-1][y]==‘.‘)    dfs(x-1,y,num+1);
24     if(ok)    return;
25     if(x<n&&s[x+1][y]==‘.‘)    dfs(x+1,y,num+1);
26     if(ok)    return;
27     if(y>1&&s[x][y-1]==‘.‘)    dfs(x,y-1,num+1);
28     if(ok)    return;
29     if(y<m&&s[x][y+1]==‘.‘)    dfs(x,y+1,num+1);
30     if(ok)    return;
31     s[x][y]=‘.‘;
32 }
33 int main()
34 {
35     int i,j;
36     scanf("%d%d%d",&n,&m,&t);
37     while(n!=0&&m!=0&&t!=0)
38     {
39         ok=false;
40         for(i=1;i<=n;i++)
41             scanf("%s",s[i]+1);
42         for(i=1;i<=n;i++)
43             for(j=1;j<=m;j++)
44             {
45                 if(s[i][j]==‘S‘)
46                 {
47                     sx=i;sy=j;
48                     s[i][j]=‘.‘;
49                 }
50                 else if(s[i][j]==‘D‘)
51                 {
52                     tx=i;ty=j;
53                     s[i][j]=‘.‘;
54                 }
55             }
56         if((abs(tx-sx)+abs(ty-sy)-t)%2==0)
57             dfs(sx,sy,0);
58         if(!ok)
59             puts("NO");
60         scanf("%d%d%d",&n,&m,&t);
61     }
62     return 0;
63 }
时间: 2024-10-02 19:09:45

Tempter of the Bone HDU - 1010的相关文章

【深搜加剪枝五】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 Bone DFS + 奇偶剪枝

奇偶剪枝: 对于从起始点 s 到达 终点 e,走且只走 t 步的可达性问题的一种剪枝策略. 如下列矩阵 : 从任意 0 到达 任意 0,所需步数均为偶数,到达任意 1 ,均为奇数.反之亦然 所以有,若s走且只走 t 步可到达e,则必有t >= abs(s.x - e.x) + abs(s.y - e.y),且 (t&1) == ((abs(s.x - e.x) + abs(s.y - e.y))&1). 否则必不可达. #include <iostream> #inclu

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): 125945    Accepted Submission(s): 33969 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 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

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 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(bfs)

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