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的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...
随机推荐
- javascript 学习笔记(权威指南)
1.数组的sort()方法默认是按照字母排序的,下面举个栗子说明: 1)全是字母: var arr =["zu","fan","an",&q ...
- java面向对象之 多态 Polymorphism
多态(Polymorphism):用我们通俗易懂的话来说就是子类就是父类(猫是动物,学生也是人),因此多态的意思就是:父类型的引用可以指向子类的对象. 1.多态的含义:一种类型,呈现出多种状态 主要讨 ...
- c语言libcurl 使用实例get/post方法+c语言字符串处理
#include <stdio.h> #include <curl/curl.h> #include <string.h> #include <ctype.h ...
- windows的命令行工具和DOS工具的区别
很多的系统管理员可能认为命令行是程序员编程用的,这是不对的,其实命令行是另一种用来管理计算机的接口.1 命令行窗口 Windows NT/Windows 2000以后的操作系统为用户提供 ...
- WISPr1.0
王桢珍 王兵 侯志强 苑红 中国移动研究院 网络技术研究所, 北京100053 摘要 本文详细介绍了WLAN国际漫游的WISPr1.0技术规范并探讨其具体实现,包括基于WISPr1.0的WLAN国 ...
- poj1665
#include <stdio.h> #include <stdlib.h> #define pi 3.1415926 int main() { float dia,tim; ...
- openstack 采用conductor的原因
供参考. Tan0同学给我的解释: 两个原因 一个是为了isolation做准备 因为升级主要就是升DB的schema 如果让compute直接读写DB,那每次升级都得升compute 现在隔离开之后 ...
- 雕爷牛腩这样判断零售未来-搜狐IT
雕爷牛腩这样判断零售未来-搜狐IT 雕爷牛腩这样判断零售未来
- Android的应用程序的异常处理2
1.自定义一个类(MaApp)继承Application 2.在清单文件中的Application选项菜单对应的name属性中添加MyApp 3.重写application中的onCreate方法 4 ...
- openvswitch安装、基本操作
一.安装,配置 //下载源码.编译.安装: #wget http://openvswitch.org/releases/openvswitch-2.3.0.tar.gz #tar -zxvf open ...