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.

SampleInput

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0

SampleOutput

NO
YES 最开始看见这题直接看了样例,以为就是询问t时间内能不能走出去,直接写了个BFS过了样例,提交上去WA了=7=,事实证明还是不能偷懒。
题意就是S当前位置,D是门,X是墙,问你能不能刚好在t时刻走到门D那里。
然后就是套DFS,本地测试炸栈,就想到了剪枝一下
 int tp = t - step - abs(dx - x) - abs(dy - y);
if(tp < || tp % == )
return ;

这就是剪枝代码,dx和dy代表门的坐标,x、y为当前坐标,step表示以及走了的步数。

当剩余时间走不到门时结束,这个好理解,难理解的是为什么为奇数是也结束。

因为从s每走一步,一定是x或y进行+1或者-1,要使得t时间刚好走到门d。

最短路径m = |dx - x| + |dy - y|;

两种情况,t < m时,肯定无法到达;

t >= m时,从S到D的行走步骤两部分组成,step = t = m + x;(m为最段路径,x为附加步数);

这x = t - m步一定是从最短路径中的某一步走出去,再回到最短路径的步数,而且二者一定是相等的。

如图,最短路径为橘色所示,附加路径为蓝色所示,把蓝色路径分为走出和走回两部分,无论你怎么添加附加路径,这两部分一定是相等的步数。

所以,x一定得为偶数才能保证从S刚好走到D。

而在剪枝中,tp就是我们的x,只有当tp为偶数时,才继续行走。

完整代码:

 #include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <bitset>
#include <ctime>
#include <fstream>
#include <limits.h>
#include <numeric> using namespace std; #define F first
#define S second
#define mian main
#define ture true #define MAXN 1000000+5
#define MOD 1000000007
#define PI (acos(-1.0))
#define EPS 1e-6
#define MMT(s) memset(s, 0, sizeof s)
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef stringstream sstm;
const int INF = 0x3f3f3f3f; int n,m,t,flag,wall;
int sx,sy,dx,dy;
char mp[][];
int vis[][];
int fx[][] = {,,-,,,,,-}; bool check(int x,int y){
if(!vis[x][y] && mp[x][y] != 'X' && x >= && y >= && x < n && y < m){
return true;
}
return false;
} void dfs(int x,int y,int step){
if(flag)
return ;
if(x == dx && y== dy && step == t){
flag = ;
return ;
} int tp = t - step - abs(dx - x) - abs(dy - y);
if(tp < || tp % == )
return ; for(int i = ; i < ; i++){
int next_x = x + fx[i][];
int next_y = y + fx[i][];
if(check(next_x,next_y)){
vis[next_x][next_y] = ;
dfs(next_x,next_y,step+);
if(flag)
return ;
vis[next_x][next_y] = ;
}
}
return ;
} int main(){
ios_base::sync_with_stdio(false);
cout.tie();
cin.tie();
while(cin>>n>>m>>t && n && m && t){
fill(vis[],vis[]+,);
MMT(mp);
flag = , wall = ;
for(int i = ; i < n; i++)
cin>>mp[i];
for(int i = ; i < n; i++){
for(int j = ; j < m; j++){
if(mp[i][j] == 'S')
sx = i,sy = j;
if(mp[i][j] == 'D')
dx = i,dy = j;
if(mp[i][j] == 'X')
wall++;
}
}
if(t < abs(dx - sx) + abs(dy - sy) || t > n*m - wall - ){
cout << "NO" << endl;
continue;
}
vis[sx][sy] = ;
dfs(sx,sy,);
if(flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
} return ;
}

Tempter of the Bone(DFS+剪枝)的更多相关文章

  1. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

  2. Tempter of the Bone dfs+剪枝

    The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...

  3. B - Tempter of the Bone(DFS+剪枝)

    The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...

  4. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  5. 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 ...

  6. Tempter of the Bone(dfs奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  7. M - Tempter of the Bone(DFS,奇偶剪枝)

    M - Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  8. hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...

  9. HDOJ.1010 Tempter of the Bone (DFS)

    Tempter of the Bone [从零开始DFS(1)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tem ...

  10. zoj 2110 Tempter of the Bone (dfs)

    Tempter of the Bone Time Limit: 2 Seconds      Memory Limit: 65536 KB The doggie found a bone in an ...

随机推荐

  1. git:将代码提交到远程仓库(码云)

    初始化 进入一个任意的文件夹(如D:\aqin_test1\) git init # 初始化,让git将这个文件夹管理起来 git add . # 收集此文件夹下的所有文件 git config -- ...

  2. 6个美观的纯CSS渐变背景代码分享(亲测有效)

    样式1 background-image: linear-gradient(160deg, #b100ff 20%,#00b3ff 80%); 样式2 background-image: linear ...

  3. laya2d 与 cad 之间的坐标转换

    坐标系基本概念 直角坐标系可分为左手坐标系与右手坐标系,cad 中用到的是右手坐标系, Laya2D 中用到的是左手坐标系, Laya3D 中使用右手坐标系. 那么如何判断二维直角坐标系是左手还是右手 ...

  4. redis最基础的入门教程

      Redis最基础入门教程 简介 Redis 简介 Redis 优势 Redis与其他key-value存储有什么不同? 字符串(Strings) 哈希(Hash) 列表(List) 集合(Sets ...

  5. 服务注册发现、配置中心集一体的 Spring Cloud Consul

    前面讲了 Eureka 和 Spring Cloud Config,今天介绍一个全能选手 「Consul」.它是 HashiCorp 公司推出,用于提供服务发现和服务配置的工具.用 go 语言开发,具 ...

  6. odoo通过actions.client进行自定义页面

    一.使用原因 由于odoo自带页面在项目开发过程中无法满足使用,需要使用到动作ir.actions.client进行自定义视图的开发,实现自定义的xml视图开发. 二.实现目标 三.开发过程 1.项目 ...

  7. js中尺寸类样式

    js中尺寸类样式 一:鼠标尺寸类样式 都要事件对象的配合 Tip:注意与浏览器及元素尺寸分开,鼠标类尺寸样式都是X,Y,浏览器及元素的各项尺寸时Height,Width 1:检测相对于浏览器的位置:e ...

  8. HTML连载34-背景关联和缩写以及插图图片和背景图片的区别

    一.背景属性缩写的格式 1.backgound:背景颜色  背景图片  平铺方式  关联方式  定位方式 2.注意点: 这里的所有值都可以省略,但是至少需要一个 3.什么是背景关联方式 默认情况下,背 ...

  9. d3.js 教程 模仿echarts柱状图

    由于最近工作不是很忙,隧由把之前的charts项目用d3.js重写的一下,其实d3.js文档很多,但是入门不是很难,可是想真的能做一个完成的,交互良好的图还是要下一番功夫的.今天在echarts找到了 ...

  10. UVALive 7264 Kejin Game 网络流+最小割

    Kejin Game 题意:一个人有一颗技能树, 现在它想修练到某个技能 (假设为x), 现在修一个技能有3种方式: 1, 将该技能的前置技能都学完了,才能学该技能. 2, 取消一个技能 与 另一个技 ...