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. windows中查看开机时间

    windows中查看开机时间     在windows下可以使用systeminfo命令来查看. 下面是网站摘录的关于windows启动了多长时间的内容 1. windows系统可以查看从开机到现在共 ...

  2. Secure Digital

    https://en.wikipedia.org/wiki/Secure_Digital#Technical_details Secure Digital (SD) is a non-volatile ...

  3. Oracle知识分类之常见规范

    命名规范 变量规范 1.入参,统一以I_前缀开头,且不能省略IN 2.出参,统一以O_前缀开头 3.局部变量,统一以V_前缀开头 4.常量,统一以CN_前缀开头 5.游标,统一以CUR_前缀开头 组成 ...

  4. php识别中文编码并自动转换为UTF-8

    原文地址:http://www.codefans.net/articles/1272.shtml php自动识别编码,若里面有中文的话,将其转换为UTF-8就最好了,因为中文在Gbk编辑情况情况下,有 ...

  5. html中submit和button的区别/ window.location.href 不跳转 的问题

    <input type="button">  <input type="submit"> 这两个的区别 是 button 不会自动提交表 ...

  6. nginx、fastCGI、php-fpm关系梳理(转)

    前言: Linux下搭建nginx+php+memached(LPMN)的时候,nginx.conf中配需要配置fastCGI,php需要安装php-fpm扩展并启动php-fpm守护进程,nginx ...

  7. .NET 串口通信中断接收,包含0X1A(作为EOF)

    .NET串口通信中将`0X1A`当做EOF处理,.NET接收到EOF会触发一次接收中断,此时事件形参`SerialDataReceivedEventArgs`值为枚举 `Eof`,其他为`Chars` ...

  8. Could not find a getter for id in class 的异常

    检查.hbm.xml里边的id是否大小写一致

  9. C++数组实现的循环队列

    #include<iostream> #include <string> /* 功能:数组实现的循环队列,C++实现,学习参考 */ using namespace std; ...

  10. C++之路进阶codevs1242(布局)

    1242 布局 2005年USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold    <:section class="hbox" ...