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. c#中用DirectShow实现媒体播放器的核心(1) DirectShow简介

    用.net做多媒体开发的似乎不多,所以网上资源也少,看的人更少.不过我的博客上居然还有几位在等新文章的人,有点出乎我的意料了.目前我已不再从事多媒体相关的工作,加入新公司至今都忙到吐血,再加上害怕水平 ...

  2. select源码分析(linux2.6.11)

    本文以tcp poll为例子来分析select的源码,下面是函数调用顺序.select--->sys_select->do_select--->sock_poll--->tcp ...

  3. who am i

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...

  4. 彻底删除sql2008r2

    一.    SQL2008卸载. 1.从控制面板卸载 1)点击计算机右下角“开始”,点击“控制面板” 2)点击“卸载程序”. 3)在程序列表中找到“Microsoft SQL Server 2008” ...

  5. 【随记】关于List集合用Linq GroupBy分组过后的遍历小记

    List<LeaderKaoQin> lstLeader = new List<LeaderKaoQin>();//一个List集合IGrouping<string, L ...

  6. Android新建项目 默认布局改为 LinearLayout

    目前此方法仅适用于eclipse 需要修改SDK 目录 android-sdk/tools/templates/activities/BlankActivity/root/res/layout 文件: ...

  7. 0xc000000f: Error attempting to read the boot configuration data

    Get the fix to “0xc000000f: error attempting to read the boot configuration data” boot error for Win ...

  8. gocode 安装

    1.安装git,将git/bin添加至PATH 2.执行go get -u github.com/nsf/gocode 3.在%GOPATH%/bin/下会生成gocode.exe

  9. svn修改密码跟账户

    在co的时候直接输入账户跟密码 svn co  ${SVNURL} ./ --username **--password *** 或者删除缓存文件,在Linux上面 删除~/.subversion/a ...

  10. WPF布局容器综合展示

    Border控件,以及几个重要要的属性:Background:背景的 Brush 对象BorderBrush:用来绘制边框BorderThickness: Border 边框的宽度,设置边框每一边的线 ...