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 ;
}

最新文章

  1. 网页变灰的CSS代码
  2. 一个书店管理系统java
  3. linux kernel input 子系统分析
  4. 加密和ssl机制细节
  5. win激活查询及修改
  6. 解决IE不支持position:fixed问题
  7. Linux下Nginx+Tomcat整合的安装与配置
  8. HDU 4971 - A simple brute force problem【最大权闭合图】
  9. 用Apache实现一个ip虚拟多个web站点
  10. QT5控件-QPushButton和QFocusFrame(按钮和焦点框)
  11. oracle 数据库 if...elsif...语句
  12. webservice和.net remoting浅谈
  13. Delphi泛型评测(30篇)
  14. rem布局下使用背景图片和sprite图
  15. Python的import嵌套
  16. centos7安装httpd和php
  17. urllib使用
  18. maven wrapper使用本地maven
  19. Oracle查询和过滤重复数据
  20. TensorFlow:tf.nn.max_pool实现池化操作

热门文章

  1. MySQL☞where与like
  2. linux下 su 与 su - 的区别和使用
  3. java设计模式之命令模式以及在java中作用
  4. java课程设计 学生管理系统
  5. oracle dg 备库不同步主库数据
  6. JS实现双击编辑可修改
  7. 怎么设置table(表格)手机端自适应宽度
  8. elasticsearch集群及filebeat server和logstash server
  9. 更换Sublime Text主题字体
  10. 大步小步算法模板题, poj2417