Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

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 desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

 
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

'X': a block of wall, which the doggie cannot enter; 
'S': the start point of the doggie; 
'D': the Door; or
'.': an empty block.

The input is terminated with three 0's. This test case is not to be processed.

 
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
 
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
 
Sample Output
NO
YES
 
题目大意:
      输入一个n*m的迷宫,和一个时间t(代表可以在迷宫中生存的最长时间,可以理解为超过时间t迷宫将会坍塌,人也会死亡),
      迷宫有一个出口,这个出口只会在第t秒的时候打开,并且人每走一步,走过的道路就会坍塌,迷宫中还有一些墙,人是不能通过的。
      问:一个人从起始地点能否刚好在第t秒的时候到达出口。
 
解题思路:
      总体思路为 dfs+剪枝  因为最后问能否刚好在第t秒到达出口,所以需要搜索所有能够走的路程,寻找是否有满足条件的情况。
      但是深搜特别费时间,直接暴力搜索一定是会超时的,所以需要通过剪枝来缩短搜索的时间。
 
      首先想到的第一个剪枝是:当剩余的最小步数 > 剩余的时间时 最后是无法在第t秒到达出口的。
      接下来需要用到的一个剪枝为:奇偶剪枝
 
奇偶剪枝:
      我们可以定义一个map数组来表示每个点的奇偶性(每个点的奇偶性为该点横纵坐标的和)
      0 1 0 1 0 1 
      1 0 1 0 1 0

      0 1 0 1 0 1 
      1 0 1 0 1 0 
      0 1 0 1 0 1 
      有这个表可以发现 从为0的格子走一步,必然走向为1的格子;同样从为1的格子走一步,必然走向为0的格子.
      即 0-->1或1-->0 必然是奇数步       0-->0或1-->1 必然是偶数步 
      so~ 当遇到从0走到0但是要求奇数步、或者从1走到0但是要求偶数步,都可以之间判断不能到达。
      应用到这个题上就是:判断剩余时间和剩余的最小步数的奇偶性是否相同。相同则有可能到达、不同则一定不能到达。
      (奇数-偶数 = 奇数    奇数-奇数= 偶数    偶数-偶数=偶数)
 
AC代码:
 #include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std; char p[][];
int a[][] = {-,,,,,,,-}; // 方向数组
int flag; // 标记是否能够在第t秒时到达出口
int n,m,t;
int sx,sy,dx,dy; // 起始地点 和 出口的地点 void dfs(int x,int y,int k)
{
int i;
if (x< || y< || x>n || y>m) // 判断是否超出边界
return ;
if (k == &&x == dx && y==dy) // 如果刚好在第t秒时到达出口 flag = 1
{
flag = ;
return ;
}
int tmp = k - abs(x-dx)-abs(y-dy);
if (tmp < || tmp&) // 判断 1、是否剩余的最小步数 > 剩余的时间 2、剩余时间和剩余的最小步数的奇偶性是否相同
return ; for (i = ; i < ; i ++)
{
if (p[x+a[i][]][y+a[i][]] != 'X') // 如果时墙则不能通过
{
p[x+a[i][]][y+a[i][]] = 'X'; // 走过之后路就坍塌了(之后不能在通过)
dfs(x+a[i][],y+a[i][],k-);
if (flag) return ; // 如果满足条件直接退出
p[x+a[i][]][y+a[i][]] = '.'; // 当前搜的这条路可能不满足条件 下次再搜别的路时要恢复原样
}
}
return ;
}
int main ()
{
int i,j;
while (scanf("%d%d%d",&n,&m,&t)!=EOF)
{
getchar();
int sum = ;
if (n==&&m==&&t==)
break;
for (i = ; i <= n; i ++)
{
for (j = ; j <= m; j ++)
{
scanf("%c",&p[i][j]);
if (p[i][j] == 'S'){sx = i; sy = j;}
else if (p[i][j] == 'D'){dx = i; dy = j;}
else if (p[i][j] == 'X') sum ++;
}
getchar();
} if (n*m-sum <= t) // 如果可以走的路的步数小于t 直接pass
{
printf("NO\n");
continue;
}
p[sx][sy] = 'X'; // 出发之后不能再回到起点
flag = ;
dfs(sx,sy,t);
if (flag)
printf("YES\n");
else
printf("NO\n");
}
return ;
}     
 

hdu 1010 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. HDU 1010 Tempter of the Bone (DFS+剪枝)

    题意:从S走到D,能不能恰好用T时间. 析:这个题时间是恰好,并不是少于T,所以用DFS来做,然后要剪枝,不然会TEL,我们这样剪枝,假设我们在(x,y),终点是(ex,ey), 那么从(x, y)到 ...

  3. hdu 1010 Tempter of the Bone(dfs暴力)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  4. 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 ...

  5. hdu 1010 Tempter of the Bone(深搜+奇偶剪枝)

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

  6. ZOJ 2110 Tempter of the Bone(DFS)

    点我看题目 题意 : 一个N×M的迷宫,D是门的位置,门会在第T秒开启,而开启时间小于1秒,问能否在T秒的时候到达门的位置,如果能输出YES,否则NO. 思路 :DFS一下就可以,不过要注意下一终止条 ...

  7. HDU 1010 Tempter of the Bone(深度+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 题意:就是给出了一个迷宫,小狗必须经过指定的步数到达出口,并且每个格子只能走一次. 首先先来介绍一下奇偶性 ...

  8. HDU 1010 Tempter of the Bone (广搜+减枝)

    题目链接 Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. How ...

  9. hdu 1010 Tempter of the Bone (奇偶性剪枝)

    题意:有一副二维地图'S'为起点,'D'为终点,'.'是可以行走的,'X'是不能行走的.问能否只走T步从S走到D? 题解:最容易想到的就是DFS暴力搜索,,但是会超时...=_=... 所以,,要有其 ...

随机推荐

  1. Compile Groovy/Spock with GMavenPlus

    在之前的博文里曾使用GMaven插件编译Groovy/Spock,这次使用GMavenplus插件,更加方便. 具体步骤 1. 导入Spock和Groovy依赖 <dependency> ...

  2. docker 安装sentry

    主页:https://sentry.io/welcome/ 环境安装 请先安装 Docker 1.10+ ,使用 CE 版本:安装文档,写的很清晰,不详述:因为国内网络环境问题,一般建议 docker ...

  3. selenium(python)用HTMLTestRunner导出报告(断言)信息的显示

    导出报告如图所示,没有显示相关信息 修改HTMLTestRunner.py文件的763-768行,注释掉if else,保留else 的uo = o 再次运行可看到信息(测试用例中的print信息也会 ...

  4. [Alpha]团队成员贡献分配规则

    设计参考 参考了往届团队sigma_rg.NewTeam.hotcode5.软剑攻城的四个评分规则后,希望该规则能队内减轻竞争的紧张气氛的同时,有具体的评分规则. 分配总则 基础分值固定为30分,将剩 ...

  5. POJ_3368 Frequent values 【线段树+区间查询】

    一.题面 POJ3368 二.分析 仍然是一道只需要区间查询不需要区间修改的线段树题. 这题的题面比较特别,它是一组非减的数组.当需要去找一段区间内出现次数最多的数字时,这些数字必然是连续的,那么就可 ...

  6. android 企业级高性能图表库 SciChart (付费)

    1.官网 https://www.scichart.com/ 2.特性 2.1 链接 https://www.scichart.com/android-chart-features/ https:// ...

  7. saltstack源码详解一

    目录 初识源码流程 入口 1.grains.items 2.pillar.items 2/3: 是否可以用python脚本实现 总结pillar源码分析: @(python之路)[saltstack源 ...

  8. CSAPP阅读笔记-存储器层次结构-第六章-P400-P462

    6.1 存储技术 1.随机访问存储器(RAM),是易失性存储器,掉电存储信息会丢失,与之相对的是非易失性存储器(ROM),它掉电后存储信息不丢失,但前者访问速度较快,但容量有限,通常只有几百或几千兆字 ...

  9. ACM java写法入门

    打2017icpc沈阳站的时候遇到了大数的运算,发现java与c++比起来真的很赖皮,竟然还有大数运算的函数,为了以后打比赛更快的写出大数的算法并且保证不错,特意在此写一篇博客, 记录java的大数运 ...

  10. jQuery 学习笔记(jQuery: The Return Flight)

    第一课. ajax:$.ajax(url[, settings]) 练习代码: $(document).ready(function() { $("#tour").on(" ...