Tempter of the Bone
Description
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
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
Sample Input
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
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的更多相关文章
- 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 ...
 - 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 ...
 - 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 ...
 - HDU 1010 Tempter of the Bone --- DFS
		
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
 - 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 ...
 - 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 ...
 - Tempter of the Bone(dfs+奇偶剪枝)
		
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
 - hdu 1010 Tempter of the Bone 深搜+剪枝
		
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
 - Tempter of the Bone(dfs奇偶剪枝)
		
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
 - ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)
		
题意 一仅仅狗要逃离迷宫 能够往上下左右4个方向走 每走一步耗时1s 每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次 问狗是否有可能逃离这个迷宫 直接DFS 直道找到满足条件的路径 ...
 
随机推荐
- PHP 获取当天 凌晨 时间戳常用代码
			
echo strtotime(date('Y-m-d')); 获取明天凌晨的时间戳代码:echo strtotime(date('Y-m-d',strtotime('+1 day'))); 附上测试代 ...
 - Sublime Text安装Package Control
			
原来Sublime Text3安装Package Control很麻烦,现在简单的方法来了! 一.简单的安装方法 使用Ctrl+`快捷键或者通过View->Show Console菜单打开命令行 ...
 - buyexpressv6
			
<script type="text/javascript"> var is_enabled = -1; var checkSubmitFlg = false; var ...
 - PHPUnit入门
			
PHPUnit是PHP语言的单元测试框架.工具,xunit单元测试工具系列成员之一,可以单独运行在Linux或windows系统下面,也可以集成到zend studio等IDE工具中. 工具下载:ht ...
 - js数组去重的hash方法
			
对于 JavaScript 数组去除重复项,现在有多种方法,其中一种是hash,如下: if (!Array.prototype.unique) { Array.prototype.unique = ...
 - Source Insight 3.X 标签插件v1.0发布
			
Source Insight可以说是一款程序员必备的开发/阅读源码工具,美中不足的是SI没有标签栏,多个源码之间切换很不方便,于是我就乘闲暇之余写了该作品sihook:标签插件;不过严格意义上来说si ...
 - DataTable/集合 转 Json
			
前端用的jqueryUI框架获取json格式数据绑定显示表格. 后端通过WebService获取的数据是DataTable. 现将获取DataTable转Json,也支持将数据集合转Json. 一.项 ...
 - c++输入一组整型数据 不知道长度 回车键结束 并将其存入数组当中
			
#include "stdafx.h"#include<iostream>using namespace std;int main(){ int a[999];int ...
 - Model View
			
#include "dialog.h" #include "ui_dialog.h" #include<QtCore> Dialog::Dialog ...
 - 【入门】 jpa--实体管理器的基本应用
			
1.新建Jpa项目 2.引入所需的jar 包 3.创建实体类 package com.watchfree.entity; import javax.persistence.Entity; import ...