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. 获取n月后的当前时间

    例如用户计算会员的到期日期时间 public static Date getMonthNextOrBeforeDate(int monthNum) { Date dNow = new Date(); ...

  2. spring-boot-plus快速快发脚手架简介

    Everyone can develop projects independently, quickly and efficiently! Introduction spring-boot-plus是 ...

  3. Selenium + python 测试环境搭建扩展-HTMLUNIT的使用

    尝试给公司的网站写每日例行检查的脚本时,不需要去打开浏览器,这是就用到HTMLUNIT的使用 HTMLUNIT是基于Selenium服务端的,所以需要selenium-server-standalon ...

  4. ajax前台数据到后台

    var username = $("#id").val(); var user={"userAccount":username,"userPasswo ...

  5. c++自由的转换string和number

    string转数字 #include <string> #include <sstream> //使用stringstream需要引入这个头文件 //模板函数:将string类 ...

  6. Hadoop RPC机制详解

    网络通信模块是分布式系统中最底层的模块,他直接支撑了上层分布式环境下复杂的进程间通信逻辑,是所有分布式系统的基础.远程过程调用(RPC)是一种常用的分布式网络通信协议,他允许运行于一台计算机的程序调用 ...

  7. Unity打包——Android和IOS

    推荐阅读:  我的CSDN  我的博客园  QQ群:704621321  我的个人博客 Android包 (1)首先需要安装Android SDK和Java JDK.SDK需要添加tools目录,JD ...

  8. JAVA 泛型中的通配符 T,E,K,V,?

    前言 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许开发者在编译时检测到非法的类型. 泛型的本质是参数化类型,也就是说所操作的数据 ...

  9. xcode删除一个项目

    退出xcode. 在Finder中删除项目文件夹.

  10. 2019nc#9

    题号 标题 已通过代码 题解/讨论 通过率 团队的状态 A The power of Fibonacci 点击查看 进入讨论 69/227 未通过 B Quadratic equation 点击查看 ...