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

题目意思很简单,但是有一个很坑的地方。就是给你一个地图,.表示可以走,X不可以走,S起点,D终点,然后给你一个步数num,然后坑点来了,问的是能不能恰好在num步的时候到达终点,步数一定要等于num。

当然,这道题,如果你直接DFS肯定是会错的。需要剪枝。

这里只要奇偶剪枝就好。

//Asimple
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
const int maxn = ;
int dx[] = {-,,,}, dy[]={,,-,};
typedef long long ll;
int n, m, num, T, k, x, y;
int endx, endy;
char Map[maxn][maxn];
bool vis[maxn][maxn], flag; bool wrang(int x, int y) {
return x< || x>=n || y< || y>=m || Map[x][y] == 'X';
} void DFS(int x, int y, int step) {
if( Map[x][y] == 'D' ) {
if( step == num ) {
flag = true;
return ;
}
return ;
}
int as = abs(x-endx)+abs(y-endy);
//奇偶剪枝
if( (num-as-step)% || as+step>num) return;
if( flag ) return ;
for(int i=; i<; i++) {
int nx = x+dx[i];
int ny = y+dy[i];
if( !wrang(nx, ny) && !vis[nx][ny] ) {
vis[nx][ny] = true;
DFS(nx, ny, ++step);
//回溯
vis[nx][ny] = false;
step--;
}
}
} void input() {
while( cin >> n >> m >> num && ( n + m + num ) ) {
for(int i=; i<n; i++) {
cin >> Map[i];
for(int j=; j<m; j++) {
if( Map[i][j] == 'S' ) {
x = i;
y = j;
}
if( Map[i][j] == 'D' ) {
endx = i;
endy = j;
}
vis[i][j] = false;
}
}
flag = false;
vis[x][y] = true;
int as = abs(endx-x)+abs(endy-y);
//奇偶剪枝
if( ( as + num )&) {
cout << "NO" << endl;
continue;
}
DFS(x,y,);
if( flag ) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
} int main(){
input();
return ;
}

Tempter of the Bone的更多相关文章

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

  2. ZOJ 2110 Tempter of the Bone

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

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

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

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

  5. Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏

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

  6. hdoj 1010 Tempter of the Bone【dfs查找能否在规定步数时从起点到达终点】【奇偶剪枝】

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

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

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

  8. hdu 1010 Tempter of the Bone 深搜+剪枝

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

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

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

  10. ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)

    题意  一仅仅狗要逃离迷宫  能够往上下左右4个方向走  每走一步耗时1s  每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次  问狗是否有可能逃离这个迷宫 直接DFS  直道找到满足条件的路径 ...

随机推荐

  1. Google内部邮件:如何进行高效的时间管理能量波动图

    Google内部邮件:如何进行高效的时间管理能量波动图 发布时间: 2015-12-16 22:16:50| 阅读数:462 views 最近,我给团队内部写了一封简单的邮件.主要内容是征询他们,如何 ...

  2. Raspberry pi 添加vnc远程桌面控制

    // 安装服务 apt-get install tightvncserver // 设置连接密码 vncpasswd // 在端口1处开启服务 tightvncserver :1

  3. iOS 10的正确解锁方式

    在iOS 10上,锁屏状态通过按下电源键点亮屏幕之后,用手指轻触Home键,实际上手机是已经解锁了的,不信请看如下截图: 虽然手机已经解锁,但与iOS 9不同的是,此时手机还处在解锁界面而没有进入主屏 ...

  4. oracle+servlet+extjs4 分页表格布局示例代码

    Log.java package com.example.entity; import java.util.Date; public class Log { private int id; priva ...

  5. java测试框架整理

    Test: Junit4+Hamcrest 不多说了,就靠着两个 import static org.hamcrest.Matchers.equalTo; import static org.juni ...

  6. [SharePoint 2013] Subscribe report within SharePoint mode

    param([string]$path, [string]$fileName, [string]$storage) $description = "Save in $storage as $ ...

  7. python 类型大小

    返回单位:字节 sys.getsizeof() import sys>>> sys.getsizeof(') >>> sys.getsizeof(') >&g ...

  8. C++ 中的sort排序用法

    STL中就自带了排序函数sortsort 对给定区间所有元素进行排序 要使用此函数只需用#include <algorithm> sort即可使用,语法描述为:sort(begin,end ...

  9. linux----------ab--性能测试工具

    ab.exe –n 10000 –c 100 localhost/index.php //其中-n代表请求数,-c代表并发数

  10. Cassandra数据类型:

    Cassandra在CQL语言层面支持多种数据类型[12]. CQL类型 对应Java类型 描述 ascii String ascii字符串 bigint long 64位整数 blob ByteBu ...