DFS/hdu 1010 Tempter of the Bone

题意

  给出一个n*m的地图,s:起点,d:终点,x:障碍物,.:可以走的格子

  给出一个时间t,求问是否可以恰好用t时间从起点走到终点。走一步为一个时间。

  注意:走过的格子不能再走

  注意2:是在时间t刚好到达,而不是时间t以内!也就是说可以在地图上绕远,反正就要在时间t到达!

分析

  很普通的深搜,但是有2点剪枝

  1.在刚读入数据后剪枝:当可以走的格子本来就比时间t还短时,必然无法在t时间走到终点

1         if (m*n-xx<=t)   //xx is the number of ‘X‘
2         {
3             printf("NO\n");
4             continue;
5         }

  2.在深搜中剪枝:1)当前坐标到终点坐标的最短路如果还比剩余时间大时,必然无法在剩余时间内走到终点

          2)当最短路与剩余时间的差为奇数时,也无法到达。因为对于同一起点终点,无论如何绕远,都只会多出奇数步

1     int delta_s=abs(x-tx)+abs(y-ty);
2     int delta_t=t-tt;
3     if (delta_t-delta_s<0  ||  (delta_t-delta_s)%2==1) return;

  

    因为刚转c++,所以给自己记录一点细节吧:while中的continue是直接跳到while上

    这个让我tle了很多次没找到错,无语

Accepted Code

 1 /*
 2     PROBLEM:hdu1010
 3     AUTHER:Nicole Lam
 4     MEMO:dfs
 5 */
 6 #include<cstdio>
 7 #include<algorithm>
 8 #include<iostream>
 9 using namespace std;
10
11 const int b[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
12
13 char a[10][10];
14 int m,n,t,sx,sy,tx,ty,flag,xx;
15
16 void dfs(int x,int y,int tt)
17 {
18     if (flag==1) return;
19     if (tt==t  &&  x==tx  &&  y==ty)
20     {
21         flag=1;
22         return;
23     }
24     if (tt>t  ||  x>n  ||  y>m  ||  x<=0  ||  y<=0) return;
25     int delta_s=abs(x-tx)+abs(y-ty);
26     int delta_t=t-tt;
27     if (delta_t-delta_s<0  ||  (delta_t-delta_s)%2==1) return;
28     for (int i=0;i<4;i++)
29     {
30         if (a[x+b[i][0]][y+b[i][1]]!=‘X‘)
31         {
32             a[x+b[i][0]][y+b[i][1]]=‘X‘;
33             dfs(x+b[i][0],y+b[i][1],tt+1);
34             if (flag==1) return;
35             a[x+b[i][0]][y+b[i][1]]=‘.‘;
36         }
37     }
38     return;
39 }
40
41 int main()
42 {
43     cin>>n>>m>>t;
44     while (n!=0 && m!=0 &&t!=0)
45     {
46         xx=0;
47         for (int i=1;i<=n;i++)
48             for (int j=1;j<=m;j++)
49             {
50                 cin>>a[i][j];
51                 if (a[i][j]==‘S‘) {sx=i;sy=j;}
52                 else if (a[i][j]==‘D‘) {tx=i;ty=j;}
53                 else if (a[i][j]==‘X‘) xx++;
54             }
55         if (m*n-xx<=t)
56         {
57             printf("NO\n");
58             continue;
59         }
60         flag=0;
61         a[sx][sy]=‘X‘;
62         dfs(sx,sy,0);
63         if (flag==1) printf("YES\n");
64         else printf("NO\n");
65         cin>>n>>m>>t;
66     }
67     return 0;
68 }
时间: 2024-11-06 23:28:33

DFS/hdu 1010 Tempter of the Bone的相关文章

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

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

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经典题+奇偶剪枝详解】

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): 75141    Accepted Submission(s): 20531 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): 129289    Accepted Submission(s): 34906 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): 82702    Accepted Submission(s): 22531 Problem Description The doggie found a bone in an ancient maze, which fascinated him a