HDU 1010:Tempter of the Bone(DFS+奇偶剪枝+回溯)
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 146511 Accepted Submission(s): 39059
Problem Description
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 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
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), 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 0's. This test case is not to be processed.
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
NO
YES
题意
给出一个N*M的矩阵和一个时间T,问能不能在时间恰好为T的时候从S走到D。X不能走'.'可以走,每次走过之后'.'就会消失。
AC代码
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e2+10;
using namespace std;
char ch[maxn][maxn];
int vis[maxn][maxn];
int ans;
int xx,yy;
int T,n,m;
int flag;
int dir[4][2]={1,0,-1,0,0,1,0,-1};
void dfs(int x,int y,int t)
{
vis[x][y]=1;
//如果正好在T时刻走到D
if(t==T&&ch[x][y]=='D')
{
flag++;
return ;
}
//奇偶剪枝,如果相差的时间和相差的曼哈顿距离的奇偶性不同,则一定无法到达
//或者相差的时间小于曼达顿距离也不行
//PS:不剪枝会超时
int res=T-t-abs(xx-x)-abs(yy-y);
if(res<0||res%2)
return ;
for(int i=0;i<4;i++)
{
int dx=x+dir[i][0];
int dy=y+dir[i][1];
if(dx>=0&&dx<n&&dy>=0&&dy<m&&ch[dx][dy]!='X'&&vis[dx][dy]==0)
{
dfs(dx,dy,t+1);
//如果能够走到D,就可以结束了,不需要回溯
if(flag)
return ;
// 回溯
vis[dx][dy]=0;
}
}
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
while(cin>>n>>m>>T&&T&&n&&m)
{
ms(vis);
flag=0;
ans=0;
int x,y;
for(int i=0;i<n;i++)
cin>>ch[i];
// 记录开始和结束的位置
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(ch[i][j]=='S')
{x=i;y=j;}
if(ch[i][j]=='D')
{xx=i;yy=j;}
}
dfs(x,y,0);
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
HDU 1010:Tempter of the Bone(DFS+奇偶剪枝+回溯)的更多相关文章
- 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+奇偶性剪枝) && 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方向走的方格上,从起点到终点的最短步数 ...
- HDU 1010 Tempter of the Bone (DFS+可行性奇偶剪枝)
<题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要 ...
- hdu 1010 Tempter of the Bone 深搜+剪枝
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 ...
- M - Tempter of the Bone(DFS,奇偶剪枝)
M - Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)
需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...
- (step4.3.1) hdu 1010(Tempter of the Bone——DFS)
题目大意:输入三个整数N,M,T.在接下来的N行.M列会有一系列的字符.其中S表示起点,D表示终点. .表示路 . X表示墙...问狗能有在T秒时到达D.如果能输出YES, 否则输出NO 解题思路:D ...
随机推荐
- Java成员变量和局部变量
Java成员变量和局部变量 一.成员变量和局部变量 二.static关键字 三.成员变量和静态变量区别 四.main函数 五.静态函数什么时候用 六.静态代码块 七.构造代码块 构造代码块先于构造函数 ...
- English trip -- Review Unit1 Personal Information 个人信息
1.重点内容进行自我介绍 What's you name? I'm Loki Where are you from? I'm Local, I'm Chengdu How old are you? t ...
- golang martini 源码阅读笔记之martini核心
继上一篇关于inject注入的笔记,理解了martini的关键核心之一:依赖注入.注入回调函数,由运行时进行主动调用执行.这一篇主要是注解martini的骨架martini.go的实现,下面先从一个简 ...
- 在 Confluence 6 中的 Jira 权限
只读(Read Only) 从你 JIRA 应用服务器上取得的用户,用户组和用户组成员.这些用户的信息只能通过你的 JIRA 服务器进行修改. https://www.cwiki.us/display ...
- hdu-1907-反nim博弈
John Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- nyoj1248(阅读理解???)
海岛争霸 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己 ...
- ~递归递归(FBI树--蓝桥)
1220: FBI树 [递归] 时间限制: 1 Sec 内存限制: 128 MB 提交: 5 解决: 4 状态 题目描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串 ...
- UVA-10118 Free Candies (DP、记忆化搜索)
题目大意:有4堆糖果,每堆有n个,有一只最多能容5个糖果的篮子.现在,要把糖果放到篮子里,如果篮子中有相同颜色的糖果,放的人就可以拿到自己的口袋.如果放的人足够聪明,问他最多能得到多少对糖果. 题目分 ...
- 第5章——使用 Razor(MVC框架视图引擎)
Razor 是MVC框架视图引擎的名称. 本章提供 Razor 语法的快速教程,以使你能够识别 Razor 表达式. 本章不打算提供 Razor 的完整参考,而将其视为一个语法速成教程.在本书的后续内 ...
- elment-ui table组件 -- 远程筛选排序
elment-ui table组件 -- 远程筛选排序 基于 elment-ui table组件 开发,主要请求后台实现筛选 排序的功能. 需求 排序 筛选 是对后台整个数据进行操作,而不是对当前页面 ...