传送门:

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

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 144191    Accepted Submission(s): 38474

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
 
Author
ZHANG, Zheng
 
题目意思:
给你一个图,给定起点和终点
问你能不能恰好在k步内到达终点
X:不能走
.:可以走
S:起点
D:终点
 
分析:
路不能重复走,时间要恰好是k
不能用BFS,因为bfs求的是最短路,而这个题最短路不一定符合要求
得用dfs
先说一下需要用到的剪枝:
1.如果当前步数大于等于k且还没有到D点,则剪掉
2.最短距离都大于k的直接输出no
3.奇偶剪枝
涉及到奇偶剪枝(说一下自己的理解):

把矩阵看成如下形式: 
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 必然是奇数步,从 0 走向 0 必然是偶数步。

所以当遇到从 0 走向 0 但是要求时间是奇数的或者 从 1 走向 0 但是要求时间是偶数的,都可以直接判断不可达!

code:
#include<bits/stdc++.h>
char s[][];
int ax,ay,bx,by,n,m,k;
int t[][]={,,-,,,,,-};//方向引导数组
int vist[][],flag;
void dfs(int x,int y,int c)
{
int i,mx,my;
if(x==bx&&y==by)//找到终点
{
if(k==c)//恰好在规定时间找到终点则标志位置1
flag=;
return;
}
if(c>=k)//超出规定时间,剪掉
return;
if(s[x][y]!='X')//可走点
{
for(i=;i<;i++)
{
mx=x+t[i][];
my=y+t[i][];
if(s[mx][my]!='X'&&mx>=&&mx<=n&&my>=&&my<=m&&!vist[mx][my])//判断能不能往这个方向走
{
vist[mx][my]=;
dfs(mx,my,c+);
vist[mx][my]=;//回退
if(flag) //注意,在找到了目标之后,就不需要再找!以往编写dfs时,没有注意这点
return;
}
}
}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&k)>&&(n+m+k))
{
int i,c;
for(i=;i<=n;i++)
{
getchar();
for(int j=;j<=m;j++)
{
scanf("%c",&s[i][j]);
if(s[i][j]=='S')
{
ax=i;//起点
ay=j;
}
if(s[i][j]=='D')
{
bx=i;//终点
by=j;
}
}
}
getchar();
memset(vist,,sizeof(vist));
if(abs(ax-bx)+abs(ay-by)>k||(ax+bx+ay+by+k)%==) // 最短距离都大于k的剪枝和奇偶剪枝
{
printf("NO\n");
continue;
}
vist[ax][ay]=;
flag=;
c=;
dfs(ax,ay,c);
if(flag==)
printf("YES\n");
else
printf("NO\n");
}
return ;
}

hdu 1010(迷宫搜索,奇偶剪枝)的更多相关文章

  1. HDU 1010 (DFS搜索+奇偶剪枝)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...

  2. hdu 1010 dfs搜索

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

  3. 杭电1010(dfs + 奇偶剪枝)

    题目: The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked ...

  4. hdu 1010 回溯加奇偶性剪枝

    普通的剪枝会超时,必须加入奇偶性剪枝. 直接上图: AC代码: #include<cstdio> #include<cstring> #include<algorithm ...

  5. hdoj--1010--Tempter of the Bone(搜索+奇偶剪枝)

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

  6. HDU 1010 Tempter of the Bone 骨头诱惑(DFS+剪枝)

    题意: 必须在第t秒走到格子D上,S为起点,D为终点,点就是可以走,X就是墙. 思路: 将迷宫外围四面都筑墙‘X’.深度搜索+奇偶剪枝,再加一个剪枝“无法在指定时间内到达”. #include < ...

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

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

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

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

随机推荐

  1. SpringMVC 工作流程

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/baidu_36697353/article/details/64444147 SpringMVC 工 ...

  2. Java 带缓冲的字节流和字符流

    输入流就是文件从硬盘到内存的中间媒介,那么输出流就是文件从内存到硬盘的中间媒介.首先来看看FileOutputStream的继承了哪些类, java.lang.Object java.io.Outpu ...

  3. hdu 1011 树形背包

    http://blog.csdn.net/libin56842/article/details/9876503 这道题和poj 1155的区别是: poj1155是边的价值,所以从边的关系入手 hdu ...

  4. pom文件解析

    Maven的依赖是使用Maven坐标来定位的,而Maven坐标主要由GAV(groupId, artifactId, version)构成.因此,使用任何一个依赖之间,你都需要知道它的Maven坐标. ...

  5. java压缩与解压

    一 概述 1.目录进入点 目录进入点是文件在压缩文件中的映射,代表压缩文件.压缩文件时,创建目录进入点,将文件写入该目录进入点.解压时,获取目录进入点,将该目录进入点的内容写入硬盘指定文件. 如果目录 ...

  6. css 样式表集合

    说到前端不得不说一下css样式 css样式是用来装饰我们的html让整个页面显得更丰富多彩,所以我们要熟悉各种css样式,本人搜集了一下 供大家参考一下 字体属性:(font) 大小 {font-si ...

  7. Java 文件上传与下载、email

    1. 文件上传与下载 1.1 文件上传 文件上传,要点: 前台: 1. 提交方式:post 2. 表单中有文件上传的表单项: <input type="file" /> ...

  8. 关于zepto 选择特定值的input 报错问题

    zepto 选择特定值的input  时,需要用单引号或双引号引用这个特定值 否则 报错

  9. C#——Visual Studio项目中的AssemblyInfo.cs文件包含的配置信息

    Visual Studio程序集项目中的AssemblyInfo.cs文件中的内容 using System.Reflection; using System.Runtime.CompilerServ ...

  10. 【Python】面向对象编程思想

    概念 "笔"作为一个抽象的概念,可以被看成是一个类.而一支实实在在的笔,则是"笔"这种类型的对象. 一个类可以有属于它的函数,这种函数被称为类的"方法 ...