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 ...
随机推荐
- Docker Container的概述
·通过Image创建(copy) ·在Image layer之上建立一个container layer(可读写) ·类比对象:类和实例(Image相当于抽象的一个类,Container相当于实例化的一 ...
- Fragment之间的交互
通常,一个活动可能包含一个或多个协同工作的Fragment以向用户展现一致的UI.在这种情况下,Fragment之间就需要彼此通信并交换数据,这是非常重要的.例如,一个Fragment可能包含了一个条 ...
- angular 模板语法(官方文档摘录)
https://angular.cn/guide/template-syntax {{}} 和"" 如果嵌套,{{}}里面求完值,""就是原意 <h3&g ...
- Hyper-v群集的仲裁模式配置依据
1,仲裁配置的原理 节点多数(推荐用于含有奇数个节点的群集) 可以承受的故障节点数为节点数的一半(四舍五入)减去一.例如,七个节点的群集可以承受三个节点出现故障. 节点和磁盘多数(推荐用于含有偶数个节 ...
- asp.net Core MVC + form validation + ajax form 笔记
asp.net Core MVC 有特别处理form,controller可以自己处理model的验证,最大的优势是写form时可以少写代码 先了解tag helper ,这东西就是element上的 ...
- R语言plot函数参数合集
最近用R语言画图,plot 函数是用的最多的函数,而他的参数非常繁多,由此总结一下,以供后续方便查阅. plot(x, y = NULL, type = "p", xlim = N ...
- Python的url解析库--urlparse
一.urlparse解析url的query并构建字典 下面的方法主要的功能: 解析url的各个部分,并能够获取url的query部分,并把query部分构建成dict. 具体的代码实现: >&g ...
- docker添加国内仓库安装iredmail
centos 7: 1.yum install docker or yum update docker sudo tee /etc/docker/daemon.json <<-'EOF'{ ...
- 移动端web开发技巧(转)
原文链接:http://liujinkai.com/2015/06/06/mobile-web-skill/ 移动端web开发技巧 这是一个最好的时代,因为我们站在潮流中:但也是一个最坏的时代,因为我 ...
- Serega and Fun CodeForces - 455D (分块 或 splay)
大意:给定n元素序列, 2种操作 将区间$[l,r]$循环右移1位 询问$[l,r]$中有多少个等于k的元素 现在给定q个操作, 输出操作2的询问结果, 强制在线 思路1: 分块 每个块内维护一个链表 ...