HDU 1010 Tempter of the Bone (ZOJ 2110) DFS+剪枝
传送门:
HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1010
ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1110
题目大意:
一只狗陷入了迷宫,他每秒必须走一步,并且不能重复走已经走过的地方,给定一个出口,要求在恰好为T的时间到达,如果可以,输出YES否则NO
不断的TLE,然后搜了下题解。
这题最漂亮的剪枝就在于奇偶的判断上。Orz
DFS过程中,设终点的坐标为door_x,door_y。当前的坐标为x,y,目标时间为target,当前时间为step
那么如果他能敲好在target-step的时间到达,那么 target - step 和 abs(door_x - x) + abs(door_y - y) 奇偶性相同。
即 ( target - step ) % 2== ( abs(door_x - x) + abs(door_y - y) ) % 2
根据奇-奇=偶,偶-偶=偶 ,可以直接写成
remain=target- (step + abs(door_x - x) + abs(door_y - y))
remain & 1 (为1的话说明remain最后一位为1,显然奇偶性不同)
其他两个剪枝看代码吧
#include<cstdio>
#include<queue>
#include<cmath>
using namespace std;
const int MAXN=10;
char maze[MAXN][MAXN];
const int dx[]={1 ,-1, 0 , 0};
const int dy[]={0 , 0 ,1 ,-1};
int start_x,start_y,door_x,door_y; bool dfs(int x,int y,int step,int target)
{
if(step>target) //剪枝1
return false; int remain=target- (step + abs(door_x - x) + abs(door_y - y));//当前和终点的距离
if(remain < 0 || remain & 1 ) //剪枝2,如果当前距离更大那么答案显然不存在
return false; //如果奇偶性不同也不行,奇-奇=偶,偶-偶=偶
//Orz想出来的人 for(int i=0;i<4;i++)
{
int nx=x + dx[i];
int ny=y + dy[i];
if(maze[nx][ny]=='.')
{
maze[nx][ny]='X';
if(dfs(nx,ny,step+1,target))
return true;
maze[nx][ny]='.';
}
else if(maze[nx][ny]=='D' && step+1==target)
return true; }
return false;
} int main()
{
int n,m,t;
while(scanf("%d%d%d",&n,&m,&t),n||m||t)
{
int empty=0;
for(int i=1;i<=n;i++)
{
scanf("%s",maze[i]+1); //一开始WA不断就是因为输入
for(int j=1;j<=m;j++)
{
if(maze[i][j]=='S')
start_x=i,start_y=j;
else if(maze[i][j]=='D')
door_x=i,door_y=j;
if(maze[i][j]=='.')
empty++;
}
} if(empty +1 < t) //剪枝3,如果可走的少于出现出口的时间,显然无解。
{
printf("NO\n");
continue;
} for(int i=0;i<=n+1;i++) //最外层直接包上X,省了等下判断越界问题。
maze[i][0]=maze[i][m+1]='X';
for(int i=0;i<=m+1;i++)
maze[0][i]=maze[n+1][i]='X'; printf("%s\n",dfs(start_x,start_y,0,t)==true? "YES":"NO");
}
}
HDU 1010 Tempter of the Bone (ZOJ 2110) DFS+剪枝的更多相关文章
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- 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 奇偶剪枝
如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...
- 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 + 奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 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 ...
- hdu 1010 Tempter of the Bone 深搜+剪枝
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 ...
- hdu 1010 Tempter of the Bone(深搜+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- Input/output subsystem having an integrated advanced programmable interrupt controller for use in a personal computer
A computer system is described having one or more host processors, a host chipset and an input/outpu ...
- 1.lombok系列1:初识lombok
转自:https://www.imooc.com/article/18156 初识lombok 官网:https://projectlombok.org/ 什么是lombok 连官网都懒得废话,只给出 ...
- sql跳过非工作日(周末和节假日)
简介:场景1:基于开始日期和工期,推算结束日期. 场景2:基于开始日期和结束日期,计算工期 注:需要自己做界面维护工作日表(s_WorkDay)和节假日表(s_SpecialDay) 涉及到的数据表 ...
- 【arc062e】Building Cubes with AtCoDeer
Description STL有n块瓷砖,编号从1到n,并且将这个编号写在瓷砖的正中央: 瓷砖的四个角上分别有四种颜色(可能相等可能不相等),并且用Ci,0,Ci,1,Ci,2,Ci,3分别表示左上. ...
- 关于Webpack详述系列文章 (第一篇)
WebPack官网地址(https://webpack-china.org/) 1. 什么是WebPack WebPack可以看做是模块打包机:它做的事情是,分析你的项目结构,找到JavaScript ...
- 【Java学习】Font字体类的用法介绍
一.Font类简介 Font类是用于设置图形用户界面上的字体样式的,包括字体类型(例如宋体.仿宋.Times New Roman等).字体风格(例如斜体字.加粗等).以及字号大小. 二.Font类的引 ...
- mysql新加入用户与删除用户详细操作命令
方法1 :使用mysql root(root权限)用户登陆直接赋权也能够创建用户 /usr/bin/mysqladmin -u root password 123456 mysql -uroot -p ...
- 图解String类型的不可变性及其原因
1.String的不可变性 String s="abcd": 上面的语句定义了一个字符串变量s.该变量指向字符串"abcd",当初始化变量s时,会在堆中为s非配 ...
- Altium Designer如何对齐原件
右边那个图标是排列菜单
- .netcore下的微服务、容器、运维、自动化发布
原文:.netcore下的微服务.容器.运维.自动化发布 微服务 1.1 基本概念 1.1.1 什么是微服务? 微服务架构是SOA思想某一种具体实现.是一种将单应用程序作为一套小型 ...