hdu1010:Tempter of the Bone

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010

转自:http://www.cnblogs.com/zhourongqing/archive/2012/04/28/2475684.html

深度优先搜索,用到了奇偶剪枝,第一次听说。。仍然是咸鱼一条

奇偶剪枝:

是数据结构的搜索中,剪枝的一种特殊小技巧。

现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点,

 

s        
|        
|        
|        
+ e

如图所示(“|”竖走,“—”横走,“+”转弯),易证abs(ex-sx)+abs(ey-sy)为此问题类中任意情况下,起点到终点的最短步数,记做step,此处step1=8;

  

s  
  +  
| +      
|        
+ e

如图,为一般情况下非最短路径的任意走法举例,step2=14;

step2-step1=6,偏移路径为6,偶数(易证);

故,若t-[abs(ex-sx)+abs(ey-sy)]结果为非偶数(奇数),则无法在t步恰好到达;

返回,false;

反之亦反。

刚开始也是写了宽搜,结果wa,看题,提取关键字!

代码:

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <map>
  3 #include <set>
  4 #include <cmath>
  5 #include <queue>
  6 #include <stack>
  7 #include <cstdio>
  8 #include <string>
  9 #include <vector>
 10 #include <cstring>
 11 #include <iostream>
 12 #include <algorithm>
 13 #include <functional>
 14 #include<string.h>
 15 using namespace std;
 16 #define rep(i,a,n) for (int i=a;i<=n;i++)
 17 #define per(i,a,n) for (int i=n;i>=a;i--)
 18 #define pb push_back
 19 #define mp make_pair
 20 #define all(x) (x).begin(),(x).end()
 21 #define fi first
 22 #define se second
 23 #define SZ(x) ((int)(x).size())
 24 typedef vector<int> VI;
 25 typedef long long ll;
 26 typedef pair<int, int> PII;
 27 const ll mod = 1000000007;
 28 // head
 29 const int MAX_N = 10;
 30
 31 int i_add[] = { 0,1,0,-1 };
 32 int j_add[] = { 1,0,-1,0 };
 33
 34 int n, m, t;
 35 int stai, staj, endi, endj;
 36 char maze[MAX_N][MAX_N];
 37 bool visited[MAX_N][MAX_N];
 38 bool res = false;
 39
 40 bool dfs(int i, int j, int d) {
 41     if (res) return true;
 42     if (i < 0 || j < 0 || i >= n || j >= m) return false;
 43     if (d > t) return false;
 44     if (maze[i][j] == ‘D‘&&d == t) return res = true;
 45     int tmp = t - d - abs(i - endi) - abs(j - endj);
 46     if (tmp & 1) return false;
 47
 48     int ii, jj;
 49     ii = i - 1, jj = j;
 50     if (!visited[ii][jj] && maze[ii][jj] != ‘X‘) {
 51         visited[ii][jj] = true;
 52         dfs(ii, jj, d + 1);
 53         visited[ii][jj] = false;
 54     }
 55     ii = i, jj = j - 1;
 56     if (!visited[ii][jj] && maze[ii][jj] != ‘X‘) {
 57         visited[ii][jj] = true;
 58         dfs(ii, jj, d + 1);
 59         visited[ii][jj] = false;
 60     }
 61     ii = i + 1, jj = j;
 62     if (!visited[ii][jj] && maze[ii][jj] != ‘X‘) {
 63         visited[ii][jj] = true;
 64         dfs(ii, jj, d + 1);
 65         visited[ii][jj] = false;
 66     }
 67     ii = i, jj = j + 1;
 68     if (!visited[ii][jj] && maze[ii][jj] != ‘X‘) {
 69         visited[ii][jj] = true;
 70         dfs(ii, jj, d + 1);
 71         visited[ii][jj] = false;
 72     }
 73     return false;
 74 }
 75
 76 int main(){
 77     while (scanf("%d %d %d", &n, &m, &t)) {
 78         if (n == 0 && m == 0 && t == 0) {
 79             break;
 80         }
 81         memset(maze, 0, sizeof(maze));
 82         //initialize
 83         memset(visited, 0, sizeof(visited));
 84         int k = 0;
 85
 86         for (int i = 0; i < n; i++) {
 87             for (int j = 0; j < m; j++) {
 88                 scanf(" %c", &maze[i][j]);
 89                 if (maze[i][j] == ‘S‘) {
 90                     stai = i, staj = j;
 91                 }
 92                 if (maze[i][j] == ‘D‘) {
 93                     endi = i, endj = j;
 94                 }
 95                 if (maze[i][j] == ‘X‘) k++;
 96             }
 97         }
 98         if (n*m - k - 1 < t)
 99         {
100             cout << "NO" << endl;
101         }
102         else {
103             dfs(stai, staj, 0);
104             if (res)
105                 cout << "YES" << endl;
106             else
107                 cout << "NO" << endl;
108         }
109     }
110     return 0;
111 }
时间: 2024-10-14 14:14:14

hdu1010:Tempter of the Bone的相关文章

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

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

HDU1010 Tempter of the Bone(回溯 + 剪枝)

题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D’代表门,‘.’代表可行走的地方,小狗每次可以选择往周围的四个方向行走,问这个小狗能否正好T步找到门. 思路: 利用回溯 + 剪枝,这道题剪枝特别重要. 剪枝一: 可以把图看成这样: 1 0 1 0 10 1 0 1 01 0 1 0 10 1 0 1 01 0 1 0 1 则假设从点 a(i + j,横纵坐标之和) 走到点 b(i + j) ,如果 a 和 b 的奇偶性相同,那么从 a 到 b 必须是偶数步

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

HDU1010 Tempter of the Bone(DFS+剪枝)

1 # include <stdio.h> 2 #include <cstdio> 3 #include <iostream> 4 #include <cstring> 5 #include <cmath> 6 using namespace std; 7 char map[8][8]; 8 int visit[8][8]; 9 struct node{ 10 int x,y; 11 }; 12 int n,m; 13 node Save,Dog

HDU1010 --- Tempter of the Bone(dfs+剪枝)

小明做了一个很久很久的梦,醒来后他竟发现自己和朋友在一个摇摇欲坠的大棋盘上,他们必须得想尽一切办法逃离这里.经过长时间的打探,小明发现,自己所在的棋盘格子上有个机关,上面写着“你只有一次机会,出发后t秒大门会为你敞开”,而他自己所在的棋盘是大小为 N*M 的长方形,他可以向上下左右四个方向移动(不可走有障碍点).棋盘中有一扇门.根据机关的提示,小明顿时明白了,他和朋友必须在第 t 秒到门口.而这一切,没有回头路!因为一旦他移动了,他刚才所在的点就会消失,并且他不能在一个点上停留超过一秒,不然格子

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 或为奇数时  肯定是不可能的  能够自己在纸上画一下 每一个点仅仅能走一次的图  走弯路的话多

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