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. 7-1 DBA顾问培训内容@20141230

    1, 逻辑读还是物理读? 查询语句的实际执行计划. F5 预计执行计划. --如何产生实际执行计划 ??. --Session收集指令.   workload repository report fo ...

  2. cocos2d-x 2.2.0 如何在lua中注册回调函数给C++

    cocos2d-x内部使用tolua进行lua绑定,但是引擎并没有提供一个通用的接口让我们可以把一个lua函数注册给C++层面的回调事件.翻看引擎的lua绑定代码,我们可以仿照引擎中的方法来做.值得吐 ...

  3. Android Studio中JNI -- 1 -- 配置方法

    1.配置NDK 1.1 下载NDK Android Studio 1.2 配 android-ndk-r10e,不同版本的Studio需要配置不同的ndk,下载完成后,随便解压放至某个文件目录下 1. ...

  4. Eclipse Git和sourceTree用法

    Eclipse Git和sourceTree用法 Eclipse Git: 提交代码到git: 1.team->Repository->pull 若没有冲突: 2.team->com ...

  5. [转]操作xml,将xml数据显示到treeview的C#代码

    XmlDocument xml = new XmlDocument(); private void Form1_Load(object sender, EventArgs e) { CreateXML ...

  6. 11-18的学习总结(DOMSecondday)

    DOM:读取访问节点对象属性 批量删除父元素下所有子节点 elem.innerHTML=""; 批量替换父元素下所有子节点 elem.innerHTML="所有子元素标签 ...

  7. yii2源码学习笔记(九)

    Application是所有应用程序类的基类,接下来了解一下它的源码.yii2\base\Application.php. <?php /** * @link http://www.yiifra ...

  8. php批量发送短信或邮件的方案

    最近遇到在开发中遇到一个场景,后台管理员批量审核用户时候,需要给用户发送审核通过信息,有人可能会想到用foreach循环发送,一般的短信接口都有调用频率,循环发送,肯定会导致部分信息发送失败,有人说用 ...

  9. core文件找不到了

    开始以为是core文件太大,设置ulimit -c unlimited  以后,再次访问,显示 ./a.out Segmentation fault (core dumped) 但是却找不到这个文件的 ...

  10. 网页端启动WinForm

    网页端启动WinForm 程序 在逛淘宝或者使用QQ相关的产品的时候,比如淘宝我要联系店家点击旺旺图标的时候能够自动启动阿里旺旺进行聊天.之前很奇怪为什么网页端能够自动启动客户端程序,最近在开发吉特仓 ...