传送门:

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. No.2一步步学习vuejs 实例demo篇

    简单应用Vue.js 的核心是一个允许采用简洁的模板语法来声明式的将数据渲染进 DOM 的系统: <div id="app"> {{ message }} </d ...

  2. Linux下查看Tomcat的控制台输出信息

    Linux下查看Tomcat的控制台输出信息 首先使用SSH连接到数据库,然后点击window创建一个new terminal, 进入tomcat/logs/文件夹下,输出控制台信息,命令如下: cd ...

  3. Vue中的静态资源管理(src下的assets和static文件夹的区别)

    ### 你可能注意到了我们的静态资源共有两个目录src/assets和static/,你们它们之间有怎样的区别呢? 资源打包 为了回答这个问题,我们需要了解webpack是如何处理静态资源的. 在所有 ...

  4. Android 触发Button按钮事件的三种方式

    1.新创建一个类 2.使用内部类 3.当多个button按钮时,为简化代码而创建的实例listener 贴代码: MainActivity.Java  文件: package com.android. ...

  5. 【2014-08-23】Beyong Coding

        be a product engineer,not be a engineer 选择比努力更重要   just code it,code it ,until you make it 缺的不是i ...

  6. python 进程池的使用

    进程同步 进程的数据是独立存在的,进程也能加锁. from multiprocessing import Process, Lock def f(l,i): l.acquire() print('he ...

  7. C语言函数指针(指向函数的指针)

    转载:http://c.biancheng.net/cpp/html/3248.html 一个函数总是占用一段连续的内存区域,函数名在表达式中有时也会被转换为该函数所在内存区域的首地址,这和数组名非常 ...

  8. Oracle从入门到精通 限定查询和排序查询的问题

    视频课程:李兴华 Oracle从入门到精通视频课程 学习者:阳光罗诺 视频来源:51CTO学院 知识点 SQL语句的执行顺序 限定符号的使用.   具体内容: 如果想要对所选择的数据进行控制,就可以使 ...

  9. window搭建svn服务器,本地提交至服务器后,直接同步

    找到版本库目录(在安装svnserver时指定的目录),如下图指定了一个版本库的hooks 在其中创建post-commit.bat文件(可先创建post-cmmit.txt再修改后缀名为bat). ...

  10. htm5 手机自适应问题 文本框被激活(获取焦点)时,页面会放大至原来尺寸。

    加上这句话就ok啦 <meta name="viewport" content="width=device-width, initial-scale=1.0, mi ...