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:奇偶剪 ...
随机推荐
- 使用concurrent.futures和ProcessPoolExecutor来替代线程和进程
concurrent.futures和ProcessPoolExecutor这两个类实现的借口分别在不同的线程或进程中执行可调用的对象,这两个类在内部维护者一个工作线程或进程池,以及要执行的队列,这两 ...
- poj 2406 Power Strings【字符串+最小循环节的个数】
Po ...
- 1.1nginx安装
1.必要软件准备 安装 pcre为了支持 rewrite 功能,我们需要安装 pcre# yum install pcre* //如过你已经装了,请跳过这一步 安装 openssl需要 ssl 的 ...
- ElasticSearch入门常用命令
基于开源项目MyAlice智能客服学习ElasticSearch https://github.com/hpgary/MyAlice/wiki/%E7%AC%AC01%E7%AB%A0%E5%AE%8 ...
- UVA 10200 Prime Time 水
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- matplotlib柱状图
import numpy as np import matplotlib.pyplot as plt size = 5 a = np.random.random(size) b = np.random ...
- DataGrid的组成论述(WPF)
DataGrid:它标示是一个整体概念,是个大容器,包含Row的感念 Column:是表格的组成成分(表格是由列组成的),它包括Header和Cell的概念
- MYSQL 索引创建与使用
可能用到索引的地方: where 子句,order by,group by 不需要创建索引的情况: 1. 表比较小 2.赋值有限的列(枚举),不要创建索引.创建的索引返回的行越少越好,此时区分度大. ...
- Spring3.2.9 + JdbcTemplate 学习
applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- 2018-2019-2 20165210《网络对抗技术》Exp6 信息搜集与漏洞扫描
2018-2019-2 20165210<网络对抗技术>Exp6 信息搜集与漏洞扫描 一.实验目标: 掌握信息搜集的最基础技能与常用工具的使用方法. 二.实验内容: 各种搜索技巧的应用 G ...