M - Tempter of the Bone(DFS,奇偶剪枝)
M - Tempter of the Bone
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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
'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
Sample Input
Sample Output
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std; char map[][];
bool vis[][];
int s_x,s_y,e_x,e_y;
int a,b,t,ok; bool check(int x,int y)
{
if (x<=||x>a||y<=||y>b)
return ;
if (map[x][y]=='X'||vis[x][y]||ok)
return ;
return ;
} void dfs(int x,int y,int time)
{
vis[x][y]=;
//printf("(%d,%d)\n",x,y);
if (x==e_x&&y==e_y&&time==t)//满足条件到终点
{
ok=;
return;
} int temp=t-time-(abs(x-e_x)+abs(y-e_y)); if (temp<)//剩余步数小于 0
return; int n_x,n_y; n_x=x,n_y=y+;
if (check(n_x,n_y))
{
dfs(n_x,n_y,time+);
vis[n_x][n_y]=;
} n_x=x+,n_y=y;
if (check(n_x,n_y))
{
dfs(n_x,n_y,time+);
vis[n_x][n_y]=;
} n_x=x,n_y=y-;
if (check(n_x,n_y))
{
dfs(n_x,n_y,time+);
vis[n_x][n_y]=;
} n_x=x-,n_y=y;
if (check(n_x,n_y))
{
dfs(n_x,n_y,time+);
vis[n_x][n_y]=;
}
} int main()
{
int i,j;
while (scanf("%d%d%d",&a,&b,&t)&&a+b+t)
{
getchar();
int wall=;
for (i=;i<=a;i++)
{
for (j=;j<=b;j++)
{
scanf("%c",&map[i][j]);
if (map[i][j]=='S')
{
s_x=i;
s_y=j;
}
if (map[i][j]=='D')
{
e_x=i;
e_y=j;
}
if (map[i][j]=='X')
wall++;
}
getchar();
} if (a*b-wall<=t)//所有路都走了还到不了终点,注意是 <=t ,可以少300ms
{
printf("NO\n");
continue;
} ok=;
memset(vis,,sizeof(vis));
int temp=t-(abs(s_x-e_x)+abs(s_y-e_y)); if (temp%==)//奇偶性相同才bfs
dfs(s_x,s_y,); if (ok)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
M - Tempter of the Bone(DFS,奇偶剪枝)的更多相关文章
- 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 ...
- Tempter of the Bone(dfs奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...
- 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 ...
- hdu Tempter of the Bone (奇偶剪枝)
学习链接:http://www.ihypo.net/1554.html https://www.slyar.com/blog/depth-first-search-even-odd-pruning.h ...
- hdu1010Tempter of the Bone(dfs+奇偶剪枝)
题目链接:pid=1010">点击打开链接 题目描写叙述:给定一个迷宫,给一个起点和一个终点.问是否能恰好经过T步到达终点?每一个格子不能反复走 解题思路:dfs+剪枝 剪枝1:奇偶剪 ...
- hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- hdoj--1010<dfs+奇偶剪枝>
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:在n*m的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...
随机推荐
- 三位一体的漏洞分析方法-web应用安全测试方法
本文转自乌云知识库 0x00 前言 节选自: http://www.owasp.org.cn/OWASP_Conference/owasp-20140924/02OWASPWeb20140915.pd ...
- SQLCMD Mode: give it one more chance
From : http://sqlblog.com/blogs/maria_zakourdaev/archive/2012/05/11/sqlcmd-mode-give-it-one-more-cha ...
- .Net——实现IConfigurationSectionHandler接口定义处理程序处理自己定义节点
除了使用.net里面提供的内置处理程序来处理我们的自己定义节点外,我们还能够通过多种方法,来自己定义处理类处理我们的自己定义节点,本文主要介绍通过实现IConfigurationSectionHand ...
- ngnix学习视频
https://www.bilibili.com/video/av36019080/?p=1
- JAVA-IO操作,字节-字符转换流
掌握OutputStreamWriter和InputStreamReader类的作用 一般操作输入输出内容的时候,就需要使用字节或字符流,但是,有些时候,需要将字符流变成字节流形式,或者字节流变成字符 ...
- [1-7] 把时间当做朋友(李笑来)Chapter 7 【从此时此刻开始改变】 摘录
大多数事情都需要提前准备,也都可以提前准备.认识到这一点本身就几乎是一切改变的起点. 任何动作演练到一定的次数,就能做到甚至在无意识的情况下都可以准确完成的地步.而他只不过是把这个原理应用到了极致而已 ...
- shll 基础讲解
http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html Shell编程基础 $# 命令行得到的参数个数 $@ 命令行得到的所有参数作 ...
- __block 和__weak 区别及使用
API Reference对__block变量修饰符有如下几处解释: //A powerful feature of blocks is that they can modify variables ...
- 分布式计算中WebService的替代方案: RPC (XML-RPC | JSON-RPC)
XML-RPC http://zh.wikipedia.org/wiki/XML-RPC XML-RPC是一个远程过程调用(远端程序呼叫)(remote procedure call,RPC)的分布式 ...
- html x
使用 Target 属性,下面的这行会在新窗口打开文档:<a href="http://www.w3school.com.cn/" target="_blank&q ...