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 ...
随机推荐
- AC大牛经典语录
超经典: 1. 为了世界的和平,为了女生的安全,我拼命做题,做题,做题! 2. A ac a day, keeps the doctor away! 3. from good to great ...
- IOS拷贝文件到沙盒
- (void)copyFileFromResourceTOSandbox { //文件类型 NSString * docPath = [[NSBundle mainBundle] pathForRe ...
- MVC4中 jquery validate 不用submit方式验证表单或单个元素
正确引入MVC4 jquery验证的相关文件 <script src="/Scripts/jquery-1.4.4.js"></script> <sc ...
- ADT "Running Android Lint" has encountered a problem
解决办法: Window--->Preferences----->Android--------> LInt Error Checking----->when saving f ...
- dojo.create\dojo.place\dojo.empty\dojo.destroy\dojo.body
1.dojo.create 1.create a node; 2.set attributes on it; 3.place it in the DOM. dojo.create(/*String| ...
- 使用dbstart 和dbshut 脚本来自动化启动和关闭数据库
使用dbstart 和dbshut 脚本来自动化启动和关闭数据库:1. 登录用户root.2. 编辑你的平台的oratab 文件.打开文件/etc/oratab:文件里数据库条目为以下格式:SID:O ...
- ios9基础知识(UI)总结
UIWindow.UILabel.UIColor.UIScreen.UIViewController.UIView.UIControl.UIButton.IBOutlet.IBAction.UISte ...
- 【转载自友盟消息推送iOS文档】在appDelegate中注册推送
1.2 基本功能集成指南 提示 请先在友盟的消息推送管理后台中创建App,获得AppKey和AppSecret 导入SDK 下载 UMessage_Sdk_All_x.x.x.zip并解压缩 导入 ...
- 003 assert简述
- LAMP介绍及安装
LAMP介绍及安装 1. LAMP是什么? LAMP,包含Linux + Apache + PHP + Mysql. LAMP适用环境 适用于追求极致稳定的WEB环境,缺点是需要消耗更多资源. 除了L ...