Tempter of the Bone(dfs+奇偶剪枝)题解
Tempter of the Bone
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.
题意:
一个n*m的矩阵,老鼠的起点在矩阵中的'S'上,终点在矩阵中的'D',其中'X'是墙,老鼠不能通过,'.'是路但是只能通过一次,过了一次之后就不能再走这个地方了,终点D在第K秒是打开,这就要求老鼠能够在第K秒是正好到达D点,如果不能就输出NO,可以的话就输出YES.
思路:
用dfs解决,但是如果剪枝没弄好会超时,新学到了一个奇偶剪枝。
奇偶剪枝:从a到b的路径大小永远是 dis=abs(ax-bx)+abs(ay-by)+偶数 。所以以此可以先一步判断
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define N 10
using namespace std;
int m,n,t,flag;
char map[N][N];
int vis[N][N],step,to[4][2]={0,1,1,0,-1,0,0,-1};
void dfs(int x,int y){
if(flag) return;
if(map[x][y]=='D' && step==t){
flag=1;
return;
}
if(step>=t) return;
for(int i=0;i<4;i++){
int fx=x+to[i][0],fy=y+to[i][1];
if(fx>=1 && fx<=n && fy>=1 && fy<=m && vis[fx][fy]==0 && map[fx][fy]!='X'){
step+=1;
vis[fx][fy]=1;
dfs(fx,fy);
step-=1;
vis[fx][fy]=0;
}
}
return;
}
int main(){
int sx,sy,ex,ey,i,j;
while(~scanf("%d%d%d",&n,&m,&t) && n+m+t){
for(i=1;i<=n;i++){
scanf("%s",map[i]+1);
for(j=1;j<=m;j++){
if(map[i][j]=='S'){
sx=i,sy=j;
}
if(map[i][j]=='D'){
ex=i,ey=j;
}
}
}
int dis=abs(sx-ex)+abs(sy-ey);
if((dis%2)!=(t%2)){ //奇偶剪枝
printf("NO\n");
continue;
}
memset(vis,0,sizeof(vis));
step=0;
flag=0;
vis[sx][sy]=1;
dfs(sx,sy);
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}
Tempter of the Bone(dfs+奇偶剪枝)题解的更多相关文章
- 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+奇偶剪枝)
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' 四 ...
- hdu Tempter of the Bone (奇偶剪枝)
学习链接:http://www.ihypo.net/1554.html https://www.slyar.com/blog/depth-first-search-even-odd-pruning.h ...
- 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 ...
- 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的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...
随机推荐
- 10 ref 和 out 之间的差别
(1) 两者都是按地址传递的,使用后都将改变原来的数值 (2) ref传进去的參数必须在调用前初始化,out不必 (3) ref传进去的參数在函数内部能够直接使用,而out不可 (4) ref传进去的 ...
- js与jQuery差别
jQuery能大大简化Javascript程序的编写,我近期花时间了解了一下jQuery.把我上手过程中的笔记和大家分享出来.希望对大家有所帮助. 要使用jQuery.首先要在HTML代码最前面加上对 ...
- jmeter测试手机号码归属地
jmeter测试手机号码归属地接口时,HTTP请求有以下两种书写方法: 1.请求和参数一同写在路径中 2.参数单独写在参数列表中 请求方法既可以使用GET方法又可以使用POST方法. 注意:“服务器名 ...
- abap开发中update module 的创建和使用
一.update module 的创建和使用 最近遇到这样一个需求,需要先删除(delete)表中的数据,再将传递过来的新数据添加(modify)到表中. 但是如果下面modify的时候出现错误,使用 ...
- Scala里面如何使用枚举
枚举通常用来定义已知数量的常量,比如月份,星期,季节等等,用过java的人都知道定义枚举的关键字是enum,在scala里面和java有所不同,来看一个完整的例子定义: object EnumTest ...
- [vue]vue-book
我们打算要做这几个模块 首页 列表 收藏 添加 home.vue --> list.vue -->app.vue --> main.js 安装环境 npm i vue-cli -g ...
- 实习培训——Servlet(7)
实习培训——Servlet(7) 1 Servlet 异常处理 当一个 Servlet 抛出一个异常时,Web 容器在使用了 exception-type 元素的 web.xml 中搜索与抛出异常类 ...
- (转)Elasticsearch分析聚合
Elasticsearch不仅仅适合做全文检索,分析聚合功能也很好用.下面通过实例来学习. 一.准备数据 {"index":{ "_index": " ...
- 项目发布脚本-go
#!/bin/bash export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin clear printf &q ...
- rsync+inotify安装配置 实时同步文件
安装 #安装inotify 工具 [root@localhost ~]# yum install inotify-tools -y 常用命令 [root@localhost ~]# inotifywa ...