传送门:

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. 初步理解require.js模块化编程

    初步理解require.js模块化编程 一.Javascript模块化编程 目前,通行的Javascript模块规范共有两种:CommonJS和AMD. 1.commonjs 2009年,美国程序员R ...

  2. <%@ page isELIgnored="false"%>的作用

    JSP 2.0的一个主要特点是它支持表达语言(expression language).JSTL表达式语言可以使用标记格式方便地访问JSP的隐含对象和JavaBeans组件,JSTL的核心标记提供了流 ...

  3. Found 1 slaves: Use of uninitialized value in printf at /usr/local/percona-toolkit/bin/pt-online-schema-change line 8489

    1. problem description: as the title show, i miss the first problem using pt-online-schema-change to ...

  4. js内存空间详细图解-笔记

    原文参考http://mp.weixin.qq.com/s/NGqdjhoU3MR9LD0yH6tKIw 栈-先进后出堆-类比成书于书架(形象),只要知道Key就可以找到value 基础数据类型(Un ...

  5. vim使用方法----转载

    转载自:http://www.cnblogs.com/itech/archive/2009/04/17/1438439.html vi/vim 基本使用方法本文介绍了vi (vim)的基本使用方法,但 ...

  6. 通过JTS源码分析Rtree(未完待续)

    前言 R树在数据库等领域做出的功绩是非常显著的.它很好的解决了在高维空间搜索等问题.它把B树的思想很好的扩展到了多维空间,采用了B树分割空间的思想,并在添加.删除操作时采用合并.分解结点的方法,保证树 ...

  7. MARS3.6 Programming

    An Assembly Language I.D.E. To Engage Students Of All Levels * A Tutorial *2007 CCSC: Central Plains ...

  8. 针对通过 SSH 连接到 Azure Linux VM 时发生的失败、错误或被拒绝问题进行故障排除

    尝试连接到 Linux 虚拟机 (VM) 时,有多种原因可能会导致安全外壳 (SSH) 错误.SSH 连接失败或被拒绝. 本文帮助用户找出原因并更正问题. 可以使用 Azure 门户.Azure CL ...

  9. cookie的初识和运用(js和jq)

    cookie是什么 cookie是浏览器提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由JavaScript对其进行控制,而并不是JavaScript本身 ...

  10. Linux 学习笔记之关机问题

    在linux领域内大多用在服务器上,很少遇到关机的操作. 正确的关机流程为:sync > shutdown > reboot > halt 关机指令为:shutdown ,你可以ma ...