Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 92693    Accepted Submission(s): 25191

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
 
Author
ZHANG, Zheng
 
Source
 
dfs,注意剪枝
 
/*
ID: LinKArftc
PROG: 1010.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ;
int n, m, t;
char mp[maxn][maxn];
bool vis[maxn][maxn]; struct Node {
int x, y, step;
Node() {}
Node(int _x, int _y, int _s) : x(_x), y(_y), step(_s) {}
} start, door; int dx[] = { -, , , };
int dy[] = { , , , - }; bool dfs(Node cur) {
if (cur.step == t) {
if (mp[cur.x][cur.y] == 'D') return true;
return false;
}
int tmp = abs(cur.x - door.x) + abs(cur.y - door.y);
if ((tmp % ) != ((t - cur.step) % )) return false;
for (int i = ; i < ; i ++) {
int xx = cur.x + dx[i];
int yy = cur.y + dy[i];
if (mp[xx][yy] == 'X' || vis[xx][yy]) continue;
if (mp[xx][yy] == 'D' && (cur.step + != t)) continue;
vis[xx][yy] = true;
if (dfs(Node(xx, yy, cur.step + ))) return true;
else vis[xx][yy] = false;
}
return false;
} int main() {
//input;
while (~scanf("%d %d %d", &n, &m, &t)) {
if (n == && m == && t == ) break;
memset(mp, 'X', sizeof(mp));
memset(vis, , sizeof(vis));
int cnt = ;
for (int i = ; i <= n; i ++) {
for (int j = ; j <= m; j ++) {
scanf(" %c", &mp[i][j]);
if (mp[i][j] != 'X') cnt ++;
if (mp[i][j] == 'S') {
start = Node(i, j, );
vis[i][j] = true;
} else if (mp[i][j] == 'D') door = Node(i, j, t);
}
}
if (t > cnt - ) {
printf("NO\n");
continue;
}
if (dfs(start)) printf("YES\n");
else printf("NO\n");
} return ;
}

HDU1010(dfs+剪枝)的更多相关文章

  1. HDU1010 DFS+剪枝

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

  2. hdu-1010 dfs+剪枝

    思路: 剪枝的思路参考博客:http://www.cnblogs.com/zibuyu/archive/2012/08/17/2644396.html  在其基础之上有所改进 题意可以给抽象成给出一个 ...

  3. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  5. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  6. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  7. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  8. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  10. LA 6476 Outpost Navigation (DFS+剪枝)

    题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...

随机推荐

  1. jmeter+ant的使用

    1.安装ant 下载ant,解压到某盘 2.配置环境变量: 变量名称 变量值 备注 ANT_HOME F:\apache-ant-1.10.3 Ant的解压路径 Path %ANT_HOME%\bin ...

  2. 剑指offer-树的子结构17

    题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) class Solution: def issubTree(self,pRoot1,pRoot2) ...

  3. Mininet实验 动态改变转发规则

    介绍 拓扑如下: 在该环境下,假设H1 ping H4,初始的路由规则是S1-S2-S5,一秒后,路由转发规则变为S1-S3-S5,再过一秒,规则变为S1-S4-S5,然后再回到最初的转发规则S1-S ...

  4. scrapy学习-爬取天天基金网基金列表

    目录 描述 环境描述 步骤记录 创建scrapy项目 设置在pycharm下运行scrapy项目 分析如何获取数据 编写代码 step 1:设置item step 2:编写spider step 3: ...

  5. iOS-UIImageView播放动画

    NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"lanya1"],[UIImage imag ...

  6. 【bzoj1878】[SDOI2009]HH的项链 树状数组

    题目描述 HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此, 他的项链变得越来越长.有一 ...

  7. [bzoj4860] [BeiJing2017]树的难题

    Description 给你一棵 n 个点的无根树.树上的每条边具有颜色. 一共有 m 种颜色,编号为 1 到 m.第 i 种颜色的权值为 ci.对于一条树上的简单路径,路径上经过的所有边按顺序组成一 ...

  8. 【NOIP模拟赛】 permutation 数学(打表)

    biubiu~~~ 这道题卡读题卡得很死......首先他告诉我们读循环的时候要顺着圈读,然后又说这个圈在数列上要以最大数开始读,而且以这样的循环的首数排序,得到的序列与原序列一样那么他就是可行序列, ...

  9. POJ3159:Candies(差分约束)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 39666   Accepted: 11168 题目链接:h ...

  10. HDU2732:Leapin' Lizards(最大流)

    Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...