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