Tempter of the Bone

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

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,k,n和m代表一个n*m的矩阵,S为起点D为终点,问能否在k步时走到终点;
题解:太坑了,刚开始以为是bfs直接敲出来提交wa了,后来发现题中说的是在第k步时到达终点,
        这就要遍历所有可能的路线了,
        注意此题需要奇偶剪枝,不然会超时(本人亲测)
奇偶剪枝:
/*
*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
*1 0 1 0 1 0
*
*如上图所示:
*从0->1和从1->0步数都是奇数
*从0->0和从1->1步数都是偶数
*则当所要求的步数是偶数是我
*们就可以舍去步数是奇数的路
*线,同样,当所要求的步数是
*奇数时,我们可以舍去步数是
*偶数的路线 即判断:
*(x2-x1+y2-y1)&1 是否 ==k&1
*/

  AC代码:

#include<stdio.h>
#include<string.h>
int x1,x2,y1,y2;
char map[10][10];
int n,m,k,ok;
int move[4][2]={0,1,0,-1,1,0,-1,0};
int judge(int r,int c)
{
if(map[r][c]=='X'||r<0||r>=n||c<0||c>=m)
return 0;
return 1;
}
void dfs(int x,int y,int step)//step记录走的步数
{
int i;
if(ok) return ;//搜索到结果
if(step>k) return ;//步数大于k
else if(step==k)
{
if(x==x2&&y==y2)
ok=1;
return ;
}
else
{
for(i=0;i<4;i++)
{
int tx=x+move[i][0];
int ty=y+move[i][1];
if(judge(tx,ty))
{
map[x][y]='X';//标记走过的位置
dfs(tx,ty,step+1);
map[x][y]='.';//回溯取消标记
}
}
}
}
int main()
{
int i,j,wall;
while(scanf("%d %d %d",&n,&m,&k),n||m||k)
{
ok=0;
wall=0;
for(i=0;i<n;i++)
scanf("%s",map[i]);
for(i=0;i<n;i++)
{
for(j=0;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(((x2-x1+y2-y1)&1)!=(k&1)||n*m-wall<=k)
{
printf("NO\n");
continue;
}
dfs(x1,y1,0);
if(ok)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

  

 

hdoj 1010 Tempter of the Bone【dfs查找能否在规定步数时从起点到达终点】【奇偶剪枝】的更多相关文章

  1. HDOJ.1010 Tempter of the Bone (DFS)

    Tempter of the Bone [从零开始DFS(1)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tem ...

  2. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

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

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

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

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

    <题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要 ...

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

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

  7. (step4.3.1) hdu 1010(Tempter of the Bone——DFS)

    题目大意:输入三个整数N,M,T.在接下来的N行.M列会有一系列的字符.其中S表示起点,D表示终点. .表示路 . X表示墙...问狗能有在T秒时到达D.如果能输出YES, 否则输出NO 解题思路:D ...

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

    需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...

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

随机推荐

  1. 三步走起 提升 iOS 审核通过率 下篇

    根据2015年的数据统计情况,并结合<苹果应用商店审核指南>,互娱 iOS 预审组通过细分将预审工作划为3大模块:客户端资源检查.应用内容检查和提审资源检查. 在上一篇文章中,Bugly ...

  2. GCD的一点理解

    大家都知道GCD 有两种队列:一种是串行队列,一种是并发队列.什么是串行队列?串行队列就是队列中的代码块一个一个按顺序执行,每当上一个代码块执行结束后下一个代码块才会执行.打个比方,如果队列是一些首尾 ...

  3. Qt Linguist的使用

    国际化的英文表述为Internationalization,通常简写为I18N,QT Linguist是一个将“tr(“”)”引号中的语言翻译成另外语言的工具 1. 创建.ts文件 在Creator中 ...

  4. C# Delegate 异步调用

    namespace ConsoleApplication22 { /// /// 异步操作 /// /// /// /// //internal Func<int,int,int> int ...

  5. c#写个基础的Socket通讯

    晚上想写点东西,想想把我刚来公司学的Sockt通讯写上来吧.要写的简单易懂点,新人们可以借鉴下哦,用控制台写. 先得说说Socket,与TCP/UDP啥关系,一直讲什么Socket通讯,TCP通讯,都 ...

  6. jquery 中 form的使用

    纠结了一下 form 表单的提交响应事件,因为在表单中,form标签会让浏览器自动提交,而我一直写的是 $(".btn").click(function()) 提交按钮的响应事件, ...

  7. IE6 Bug overflow:hidden失效

    下面就是我所收集或遇到的IE6 Bug之一:overflow:hidden失效 当父元素的直接子元素或者下级子元素的样式拥有position:relative属性时,父元素的overflow:hidd ...

  8. python判断一个数字是整数还是浮点数

    在网上闲逛,发现了一个python的用法

  9. 2016030102 - Ubuntu软件安装与删除相关命令

    apt-get, dkpg 常用命令: 安装软件命令: apt-get install softname1 softname2 softname3…… 卸载软件命令: apt-get remove s ...

  10. 有关collection中的一些数据结构

    Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同的元素 ...