题目链接:

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

题目描述:
根据地图,'S'为开始位置,'D'为门的位置,' . '为空地,'X'为墙,不能经过,问:在指定的时间,是否能到达'门'的位置。注意:路不可以重复经过,时间也要刚好是 t ,不能少.

思路:

此处不能用BFS,因为时间要恰好为t,还是得用DFS,不过需要剪枝才能过。

奇偶剪枝:

从一个点到达另外一个点的最短路径长度(时间)可以根据两点坐标求出,路径长度(非最短)与最短路径的长度同奇偶,它们的差一定是偶数!举个例子,就像两个偶数的差差是偶数,两个个数的差也是偶数.

此处还有一个剪枝:
设墙的数目为wall,如果wall + t >= n * m,一定到达不了,因为大于号显然成立,这里主要讨论等号的情况。

比如下图

3 3 1

SDX

XXX

XXX

只需要一步就可以到达,此时wall = 7, t = 1,wall + t < n * m,有可行解,这是由于有S,D的存在,所以如果有可行解,wall的数目一定会比n*m-t要小,等于的话是没有可行解的。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
typedef long long ll;
const int INF = <<;
const int maxn = ;
int T, cases;
int n, m, k, x1, y1, x2, y2;
char Map[][];
int dir[][] = {,,,,-,,,-};
bool dfs(int x, int y, int time)//到x1, y1点,花费时间time
{
if(x < || x >= n || y < || y >= m || time > k)return false;
//剪枝
if(time == k && x == x2 && y == y2)return true;
int mintime = k - time - abs(x - x2) - abs(y - y2);
if(mintime < )return false;//最优性剪枝,当前沿着最优路径走,还是会超过k秒,返回假
if(mintime & )return false;//奇偶剪枝,相减的时间是奇数的话,在k秒的时候一定到不了终点 for(int i = ; i < ; i++)
{
int xx = x + dir[i][];
int yy = y + dir[i][];
if(Map[xx][yy] != 'X')
{
Map[xx][yy] = 'X';
if(dfs(xx, yy, time + ))return true;
Map[xx][yy] = '.';
}
}
return false;
}
int main()
{
while(cin >> n >> m >> k && (n + m + k))
{
int wall = ;
for(int i = ; i < n; i++)
{
cin >> Map[i];
for(int j = ; j < m; j++)
{
if(Map[i][j] == 'S')x1 = i, y1 = j;
else if(Map[i][j] == 'D')x2 = i, y2 = j;
else if(Map[i][j] == 'X')wall++;
}
}
if(wall + k >= n * m)
///这里是一个特别好的剪枝,如果墙的数目+步数>=n*m,一定不可能完成任务
///重点考虑等号,除了墙还有'S'和'D',墙的数目最多为n*m-k-1(距离,比如k = 1时,S和D相邻,其他都是墙,此时墙的数目最多为n*m-2,满足上述式子)
{
printf("NO\n");
continue;
}
Map[x1][y1] = 'X';//先设置成X表示该点不能再经过
if(dfs(x1, y1, ))printf("YES\n");
else printf("NO\n");
}
}

hdu1010 Tempter of the Bone---DFS+奇偶剪枝的更多相关文章

  1. hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  2. Tempter of the Bone(dfs奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  3. M - Tempter of the Bone(DFS,奇偶剪枝)

    M - Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  4. hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...

  5. HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...

  6. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

  7. hdu Tempter of the Bone (奇偶剪枝)

    学习链接:http://www.ihypo.net/1554.html https://www.slyar.com/blog/depth-first-search-even-odd-pruning.h ...

  8. hdu1010 Tempter of the Bone(深搜+剪枝问题)

    Tempter of the Bone Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission( ...

  9. hdu1010Tempter of the Bone(dfs+奇偶剪枝)

    题目链接:pid=1010">点击打开链接 题目描写叙述:给定一个迷宫,给一个起点和一个终点.问是否能恰好经过T步到达终点?每一个格子不能反复走 解题思路:dfs+剪枝 剪枝1:奇偶剪 ...

  10. hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...

随机推荐

  1. poj-3660-cows contest(不懂待定)

    Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming con ...

  2. VB求最大公约数的两个例子

    VB求最大公约数的两个算法 Private Sub Command1_Click() Dim a As Long, b As Long a = InputBox("请输入要求最大公约数的整数 ...

  3. 【Python】 迭代器&生成器

    迭代器 任何一个类,只要其实现了__iter__方法,就算是一个可迭代对象.可迭代对象的__iter__方法返回的对象是迭代器,迭代器类需要实现next方法.一般来说,实现了__iter__方法的类肯 ...

  4. 几条常见的数据库分页 SQL 语句

    SQL Server 先从想要的数据处理加上Row_number()来为数据的row加上一个RowNum作为有多少条数据,然后再用BETWEEN来分隔 with t1 as (select * ,  ...

  5. PHP 相对完整的分页

    效果链接http://love.bjxxw.com/oejiaoyou/pubu/zhaopian.php php 分页 <?php /* * * * 说明 吉海波 2015/9/17 * $p ...

  6. java基础笔记(4)----数组

    介绍: 数组是一种数据类型,是引用类型,是一块连续的内存空间,用于存储和管理相同类型的多个数据. 定义:-- > 数组的声明方式 先声明,在开辟内存空间--> int [] a; a=ne ...

  7. poj2991 Crane(线段树+集合)白书例题

    题目大意:起重机有n节,题目给出要调节的k节,每节调节成x度,求最后底部的起重机的坐标(最顶上的起点为(0,0)). 分析:一开始我看白书,看不懂他那个向量旋转的坐标是怎么来的,翻了很多博客,才发现, ...

  8. 构造函数与析构函数(construction undergoing)

    构造函数和析构函数 一.构造函数: 1.普通构造函数:在对象被创建时利用特定的值构造对象,将对象初始化到一个特定的状态. 特性:构造函数的函数名和类名相同:没有返回值:在对象被创建时被自动调用:如果有 ...

  9. web面试题

    1.你做的页面在哪些流览器测试过?这些浏览器的内核分别是什么? Ie(Ie内核) 火狐(Gecko) 谷歌(webkit) opear(Presto)

  10. Beta冲刺第七天

    一.昨天的困难 没有困难. 二.今天进度 1.林洋洋:MD图片上传,修复权限问题,修复本地存储判空问题,修复协作申请没有过滤问题. 2.黄腾达:添加文件链接和邀请链接复制功能,协作树界面优化. 3.张 ...