HDOJ1010 (DFS+奇偶剪枝)
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 107043 Accepted Submission(s): 29107
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.
'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.
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAXN=;
int n,m,T;
char mz[MAXN][MAXN];
int vis[MAXN][MAXN];
int sy,sx,gy,gx;
int dy[]={,,,-};
int dx[]={,,-,};
bool dfs(int y,int x,int step)
{
if(y==gy&&x==gx&&step==T)
{
return true;
}
int rem=T-step;
int need=abs(y-gy)+abs(x-gx);
if(rem<need||(rem-need)%!=)//奇偶剪枝:迂回,若能到达终点,那么至少所需的步数与还剩余的步数奇偶性相同
{
return false;
}
for(int i=;i<;i++)
{
int ny=y+dy[i];
int nx=x+dx[i];
if(<=ny&&ny<n&&<=nx&&nx<m&&mz[ny][nx]!='X')
{
if(!vis[ny][nx])
{
vis[ny][nx]=;
if(dfs(ny,nx,step+))
{
return true;
}
vis[ny][nx]=;
}
}
}
return false;
}
int main()
{
while(cin>>n>>m>>T&&(n+m+T)!=)
{
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>mz[i][j];
if(mz[i][j]=='S')
{
sy=i;
sx=j;
}
else if(mz[i][j]=='D')
{
gy=i;
gx=j;
}
}
}
vis[sy][sx]=;
if(dfs(sy,sx,))
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
return ;
}
HDOJ1010 (DFS+奇偶剪枝)的更多相关文章
- hdoj--1010<dfs+奇偶剪枝>
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:在n*m的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...
- 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 ...
- 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 ...
- 杭电1010(dfs + 奇偶剪枝)
题目: The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked ...
- 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+奇偶剪枝+回溯)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu1010Tempter of the Bone(dfs+奇偶剪枝)
题目链接:pid=1010">点击打开链接 题目描写叙述:给定一个迷宫,给一个起点和一个终点.问是否能恰好经过T步到达终点?每一个格子不能反复走 解题思路:dfs+剪枝 剪枝1:奇偶剪 ...
随机推荐
- VS中一个强大的功能,将Json或者XML黏贴为类
有时候需要传递json,或者是json结构复杂,看的杂乱无章,我们可以将这个json复制下来,然后将它写成类的形式,VS中已经帮我们很好的实现了这个功能,我们只需要选择 编辑===>> ...
- CentOs linux安装SVN服务
SVN服务器有2种运行方式:1.独立服务器(例如:svn://xxx.com/xxx):2.借助apache (例如:http://svn.xxx.com/xxx):为了不依赖apache,我选择 ...
- Centos6.5下ElasticSearch1.4.4的安装
1.下载ElasticSearch 官网地址 https://www.elastic.co/ 2.安装elasticsearch-1.4.4.tar.gz tar -zxvf elasticsearc ...
- jar 冲突解决方案
val urlOfClass = classOf[Nothing].getClassLoader.getResource("org/slf4j/spi/LocationAwareLogger ...
- Grid_自绘
ZC: 测试使用的控件是 Delphi7版本的 TAdvStringGrid(第3方控件) “DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: ...
- iOS中,Framework和.a的打包及使用
最近在做一个小项目,需要给客户一个demo测试,有一部分核心代码暂时不想让客户知道,就想到了打包成framework或.a库.库有两种: 静态库:.a和.framework 动态库:.tbd和.fra ...
- BZOJ 2111 [ZJOI2010]Perm 排列计数:Tree dp + Lucas定理
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2111 题意: 给定n,p,问你有多少个1到n的排列P,对于任意整数i∈[2,n]满足P[i ...
- orale存储技术
一.存储的主要作用 1.海量存储 阵列存储 光盘存储 磁带存储 数据迁移 文件服务器,跨平台文件共享 2.容灾 数据备份及恢复:磁带.光盘.阵列 数据复制:本地镜像.远程镜像 3.高 ...
- webservice 传输数据过大,解析失败
ERROR 错误信息: 已超过传入消息(65536)的最大消息大小配额.若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性. 错误场景: webservice 服 ...
- 项目管理理论与实践(4)——UML应用(上)
本篇文章介绍UML的相关知识.参考<UML从入门到精通> 一.UML综述 1. UML简介 统一建模语言(UML)是一个通用的可视化建模语言,用于对软件进行描述.可视化处理.构造和建立软件 ...