Tempter of the Bone(dfs奇偶剪枝)
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 92175 Accepted Submission(s): 25051
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.
| s | ||||
| | | ||||
| | | ||||
| | | ||||
| + | — | — | — | e |
| s | — | — | — | |
| — | — | + | ||
| | | + | |||
| | | ||||
| + | — | — | — | e |
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define mem(x,y) memset(x,0,sizeof(x))
const int MAXN=;
int disx[]={,,,-};
int disy[]={,-,,};
int N,M,T,e_x,e_y;
char map[MAXN][MAXN];
int vis[MAXN][MAXN];
int ans;
void dfs(int x,int y,int sec){
if(ans)return;
if(map[x][y]=='D'){
// printf("%d**",sec);
if(sec==T)ans=;
return;
}
int temp=T-abs(e_x-x)-abs(e_y-y)-sec;//奇偶剪枝。。。
if(temp<||temp&)return; for(int i=;i<;i++){
int nx,ny;
nx=x+disx[i];ny=y+disy[i];
if(nx<||ny<||nx>=N||ny>=M||vis[nx][ny]||map[nx][ny]=='X')continue;//>=写错了,错了半天。。。
if(sec+>T)continue;
vis[nx][ny]=;
dfs(nx,ny,sec+);
vis[nx][ny]=;
}
}
int main(){
while(scanf("%d%d%d",&N,&M,&T),N||M||T){
for(int i=;i<N;i++)scanf("%s",map[i]);
int sx,sy;
int wall=;
for(int x=;x<N;x++)for(int y=;y<M;y++)
if(map[x][y]=='S')sx=x,sy=y;
else if(map[x][y]=='D')e_x=x,e_y=y;
else if(map[x][y]=='X')wall++;
mem(vis,);vis[sx][sy]=;
ans=;
if(T<N*M-wall)dfs(sx,sy,);
if(ans)puts("YES");
else puts("NO");
}
return ;
}
bfs wa代码扔着留念吧:
#include<stdio.h>
#include<string.h>
const int MAXN=;
#include<queue>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
struct Node{
int x,y,sec;
};
char map[MAXN][MAXN];
int disx[]={,,,-};
int disy[]={,-,,};
int vis[MAXN][MAXN];
int N,M,T;
bool bfs(int sx,int sy){
queue<Node>dl;
Node a,b;
mem(vis,);
vis[sx][sy]=;
a.x=sx;a.y=sy;a.sec=;
dl.push(a);
while(!dl.empty()){
a=dl.front();
dl.pop();
for(int i=;i<;i++){
b.x=a.x+disx[i];b.y=a.y+disy[i];b.sec=a.sec+;
if(b.x<||b.y<||b.x>=N||b.y>=M||vis[b.x][b.y]==||map[b.x][b.y]=='X')continue;
if(b.sec>T)continue;
if(map[b.x][b.y]=='D'){
if(b.sec==T)return true;
continue;
}
vis[b.x][b.y]=;
dl.push(b);
}
}
return false;
}
int main(){
while(scanf("%d%d%d",&N,&M,&T),N|M|T){
for(int i=;i<N;i++)scanf("%s",map[i]);
int sx,sy;
for(int x=;x<N;x++)for(int y=;y<N;y++)
if(map[x][y]=='S')sx=x,sy=y;
if(bfs(sx,sy))puts("YES");
else puts("NO");
}
return ;
}
Tempter of the Bone(dfs奇偶剪枝)的更多相关文章
- 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 ...
- M - Tempter of the Bone(DFS,奇偶剪枝)
M - Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...
- hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...
- hdu Tempter of the Bone (奇偶剪枝)
学习链接:http://www.ihypo.net/1554.html https://www.slyar.com/blog/depth-first-search-even-odd-pruning.h ...
- hdu1010Tempter of the Bone(dfs+奇偶剪枝)
题目链接:pid=1010">点击打开链接 题目描写叙述:给定一个迷宫,给一个起点和一个终点.问是否能恰好经过T步到达终点?每一个格子不能反复走 解题思路:dfs+剪枝 剪枝1:奇偶剪 ...
- hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- hdoj--1010<dfs+奇偶剪枝>
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:在n*m的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...
随机推荐
- PHP设计模式之单例模式(数据库访问)
1.什么是单例模式? 作为对象的创建模式,单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统全局地提供这个实例.它不会创建实例副本,而是会向单例类内部存储的实例返回一个引用. 2.单例模式的 ...
- [LeetCode]题解(python):150-Evaluate Reverse Polish Notation
题目来源: https://leetcode.com/problems/evaluate-reverse-polish-notation/ 题意分析: 给定一个数组,用这个数组来表示加减乘除,例如 [ ...
- nginx-configure执行大致流程
1,configure 命令行参数处理 2,初始化各种文件路径 3,分析源码结构 4,生成编译过程中所需路径 5,准备 .h,.err等编译所需文件 6,写入命令行参数 7,检测环境(系统,编译器,第 ...
- c++中vector与list的区别
c++标准库中,容器vector和list都可以用来存放一组类型相同的数据.而且二者不同于数组的一点是,支持动态增长.但它们还是有有几点不同 (1) vector是顺序表,表示的是一块连续的内存,元 ...
- 深入学习PE文件(转)
PE文件是Win32的原生文件格式.每一个Win32可执行文件都遵循PE文件格式.对PE文件格式的了解可以加深你对Win32系统的深入理解. 一. 基本结构. 上图便是PE文件的基本结构.(注意:DO ...
- 製程能力介紹(SPC introduction) ─ Cp之製程能力解釋
Cp之製程能力解釋 從常態分配的特性來看,在群體中 ±3σ(標準差) 之範圍內的值,應包含群體全部的 99.73%.也就是說,若以 6σ為單位,就可以代表整個分布的範圍,但是有 0.27% (2700 ...
- IE添加信任站点并设置允许ActiveX控件的VBS脚本
Set objFSO = CreateObject("Scripting.FileSystemObject") Set WSHShell = CreateObject(" ...
- 04737_C++程序设计_第7章_类模板与向量
例7.1 使用类模板的实例. 例7.2 求4个数中最大值的类模板程序. #include <iostream> using namespace std; template <clas ...
- python setattr(),getattr()函数
setattr(object,name,value): 作用:设置object的名称为name(type:string)的属性的属性值为value,属性name可以是已存在属性也可以是新属性. get ...
- codeigniter使用mongodb/redis
ci2.x版本,使用mongodb,需要安装pecl-php-mongo扩展(github上很多扩展已不可用,找到个可用版本纪录于此),添加到php.ini中 使用如下 public function ...