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的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...
随机推荐
- struts中简单的校验
Struts中简单的校验 “计应134(实验班) 凌豪” Struts2校验简要说明:struts2中通常情况下,类型转换要在数据校验之前进行.类型转换其实也是基本的服务器端校验,合法数据必然可以通过 ...
- 5 Logistic回归(二)
5.2.4 训练算法:随机梯度上升 梯度上升算法:在每次更新回归系数时都需要遍历整个数据集,在数十亿样本上该算法复杂度太高. 改进方法:随机梯度上升算法:一次仅用一个样本点更新回归系数. 由于可以在新 ...
- 2.java.lang.NullPointerException(空指针异常)
调用了未经初始化的对象或者是不存在的对象 经常出现在创建图片,调用数组这些操作中,比如图片未经初始化,或者图片创建时的路径错误等等.对数组操作中出现空指针, 即把数组的初始化和数组元素的初始化混淆起来 ...
- ODI KM二次开发手册
ODI KM二次开发手册 分类: ODI(16) 目录(?)[+] 1 引言 1.1 编写目的 本手册面向的读者对象为具备数据集成业务知识及对ODI操作了解的开发人员,作为其完成基于ODI基础上K ...
- Kill 正在执行的存储过程
1.找到正在执行的存储过程的 sid ,serial# select b.sid,b.SERIAL#,a.OBJECT, 'alter system kill session ' || ''' ...
- J2SE知识点摘记(七)
1. 枚举的用法 enum 枚举名{枚举值表标};例子:"enum weekday{sun,mon,tue,wed,fri,sat}a,b,c;" For循环语句中使 ...
- Oracle EBS-SQL (SYS-2): sys_在线用户查询.sql
SELECT fs.USER_NAME, fu.description, fs.RESPONSIBILITY_NAME, fs.USER_FORM_NAME, ...
- 如何評鑑一家SMT代工廠
我們一般稱專業的「電子代工廠」為 EMS(Electronics Manufacturing Service,電子製造服務業) 或 CM(Contract Manufacturer,合同製造廠),這些 ...
- 关于ajax中async参数的感悟
async,这个参数默认为true. 就是异步去处理信息. 当把它设置为false的时候,就是同步去处理数据了. var current_lead_id = '<?php echo $curre ...
- C语言入门(7)——自定义函数
C源程序是由函数组成的.虽然在C语言入门系列前面几篇的程序中大都只有一个主函数main(),但实用程序往往由多个函数组成.函数是C源程序的基本模块,通过对函数模块的调用实现特定的功能.C语言中的函数相 ...