hdu 1010 Tempter of the Bone(dfs暴力)
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 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.
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T ( < N, M < ; < T < ), 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 's. This test case is not to be processed.
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
S.X.
..X.
..XD
.... S.X.
..X.
...D
NO
YES
很久以前做过的一道题目,最近有学弟来问,所以又重新做了一遍。
题目要求的是 刚好在T时间走完了到达门的位置,并不是求最短到达的时间,所以不能用bfs。用dfs暴力深搜搞定。
此题要vis回溯,还要有两个剪枝。其中最重要的一个剪枝是奇偶剪枝,没了这个直接超时。如下所示:
if((T%2)!=(abs(s.x-ed.x)+abs(s.y-ed.y))%2) return;//奇偶剪枝,很重要!!!
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 10
#define inf 1e12
int n,m,t;
char mp[N][N];
int vis[N][N];
struct Node{
int x,y;
}st,ed;
int flag;
int dirx[]={-,,,};
int diry[]={,,-,};
void dfs(Node s,int T){ if(flag) return;
if(abs(s.x-ed.x) + abs(s.y-ed.y)>T) return;
//if((T-abs(s.x-ed.x) - abs(s.y-ed.y))%2 ) return;//奇偶剪枝,很重要!!!
if((T%)!=(abs(s.x-ed.x)+abs(s.y-ed.y))%) return;//奇偶剪枝,很重要!!!
if(T==){
if(s.x==ed.x && s.y==ed.y){
flag=;
printf("YES\n");
return;
}
}
Node tmp;
for(int i=;i<;i++){
tmp.x=s.x+dirx[i];
tmp.y=s.y+diry[i];
if(tmp.x< || tmp.x>=n || tmp.y< || tmp.y>=m ) continue;
if(vis[tmp.x][tmp.y]) continue;
if(mp[tmp.x][tmp.y]=='X') continue;
vis[tmp.x][tmp.y]=;
dfs(tmp,T-);
vis[tmp.x][tmp.y]=;
} }
int main()
{
while(scanf("%d%d%d",&n,&m,&t)==){
if(n== && m== && t==){
break;
}
for(int i=;i<n;i++){
scanf("%s",mp[i]);
for(int j=;j<m;j++){
if(mp[i][j]=='S'){
st.x=i;
st.y=j;
}
if(mp[i][j]=='D'){
ed.x=i;
ed.y=j;
}
}
}
memset(vis,,sizeof(vis));
vis[st.x][st.y]=;
flag=;
dfs(st,t);
if(flag==){
printf("NO\n");
}
}
return ;
}
hdu 1010 Tempter of the Bone(dfs暴力)的更多相关文章
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- 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+奇偶剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...
- hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...
- (step4.3.1) hdu 1010(Tempter of the Bone——DFS)
题目大意:输入三个整数N,M,T.在接下来的N行.M列会有一系列的字符.其中S表示起点,D表示终点. .表示路 . X表示墙...问狗能有在T秒时到达D.如果能输出YES, 否则输出NO 解题思路:D ...
- HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)
需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...
- HDU 1010 Tempter of the Bone (DFS+可行性奇偶剪枝)
<题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要 ...
- HDOJ.1010 Tempter of the Bone (DFS)
Tempter of the Bone [从零开始DFS(1)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tem ...
- 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 ...
随机推荐
- nginx+vaadin配置
nginx+Vaadin的特殊性在于配置WEBSOCKET或LONG_POLLING.网上资料不多,自己多次尝试配置都不成功,后来终于找到这篇说明才得以配置成功,使用效果不错,介绍如下. 1./etc ...
- VS2015 MVC5项目部署
刚看到一个年初的一个帖子说VS2015新建的MVC5项目部署后报错,自己捣鼓了一下,发现是Roslyn编译器的错误,简单处理后运行成功,分享如下: 新建一个MVC5的项目,保持不要动,执行以下几个步骤 ...
- Beauty of Array(模拟)
M - M Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Status P ...
- 【Android】使用FrameLayout布局实现霓虹灯效果
FrameLayout是五大布局中最简单的一个布局. 在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置. 它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的 ...
- qt获取本机网络信息
networkinformation.h #include<QtGui/QWidget> #include<QLabel> #include<QPushButton> ...
- .net如何后台批量删除
button_Click(Sender sender,Event e){foreach (DataListItem item in DataList1.Items){CheckBox cbox=(Ch ...
- if分支的四种形式
分支:——四种if一.if(条件表达式){} 二.if(条件表达式){}else{} 三.if(条件表达式){}else if(条件表达式){}else if(条件表达式){}....else{} 四 ...
- 使用SqlBulkCopy批量插入数据
static void Main(string[] args) { //定义与目标表结构相同的DataTable DataTable dataTable = new DataTable(); data ...
- jquery初学者易犯的错误
1 获取类或者对象的时候,忘记写“#”或者“.” 错误案例: $(document).ready(function(){ $("btn1").click(function(){ a ...
- SQL SERVER中如何格式化日期(转)
原文地址:http://blog.sina.com.cn/s/blog_95cfa64601018obo.html 1. SELECT convert(varchar, getdate(), 10 ...