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. 配置MyEclipse+Hibernate连接Sql Server 2008出错

    下文主要是讲述最近配置MyEclipse连接Sql Server 2008时遇到的一个问题,而不关注如何配置Sql Server 2008支持TCP/IP连接.Hibernate如何操作Sql Ser ...

  2. 利用maven的resources、filter和profile实现不同环境使用不同配置文件

    基本概念说明(resources.filter和profile): 1.profiles定义了各个环境的变量id 2.filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profil ...

  3. .NET开源工程推荐(Accord,AForge,Emgu CV)

         本人用C#开发了一些项目,下面的开源工程给了我很大的帮助——详细的源代码介绍加丰富的实例运用,是非常不错的学习资源,分享给大家,同时附上我的相关开发项目.    Accord.NET The ...

  4. windows phone 之笔势

    笔势: Windows Phone 用户可以使用触控笔势与他们的手机进行交互.触控笔势被定义为用户在触摸屏上使用单个或多个手指发起的运动.Windows Phone 上支持的控件都可以识别笔势.这些控 ...

  5. 自定义QToolButton

    最近做界面需要添加很多工具栏按钮,所以自己定义了一个Button 直接上代码 SettingButton.cpp//设置Button的一些参数 #include "SettingButton ...

  6. css重置样式表reset.css

    body, h1, h2, h3, h4, h5, h6, hr, p, blockquote,/* structural elements 结构元素 */ dl, dt, dd, ul, ol, l ...

  7. javascript图片预先加载

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. 轻松使用px为单位开发移动端页面

    研究移动端页面已经有许久了,一直执着于rem来开发,不谈性能怎么样,单从工作效率上看影响了不少,首先要固定设计稿的宽度,一般都是固定在640px,然后在根据根目录的字体大小来计算出每个元素的rem的值 ...

  9. RHEL 7特性说明(六):集群

    来自:Linux中国  2014-07-16 00:00:00  ed Hat Enterprise Linux 7.0 是 Red Hat 的下一代操作系统完整套件,旨在用于关键任务企业级计算以及顶 ...

  10. LightOJ_1038 Race to 1 Again

    题目链接 题意: 给一个数n, 每次操作是随机的选择一个[1,N]区间内能够被n整除的数进行除法, 然后得到一个新的n. 问n变成1时的期望操作次数. 思路: 设E[n] 为 当数为x时, 变成 1 ...