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 ...
随机推荐
- BZOJ2882
传送门:BZOJ2882(权限题) 最小表示法的模板. 传送门:周神论文 代码上的小细节见下. #include <cstdio> #include <cstdlib> #in ...
- js40---享元模式
/** * 享元模式是一个为了提高性能(空间复杂度)的设计模式 * 他使用与程序会生产大量的相类似的对象是耗用大量的内存的问题 */ (function(){ /** * 制造商 * 型号 * 拥有者 ...
- Date类的用法
package example; import java.text.DateFormat; import java.text.ParseException; import java.text.Simp ...
- 1.3 Quick Start中 Step 7: Use Kafka Connect to import/export data官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Step 7: Use Kafka Connect to import/export ...
- C#截取指定长度中英文字符串方法 (修改)
public static string GetFirstString(string stringToSub, int length) { Regex regex = new Regex(" ...
- Mysql 简介二
Mysql 数据库引擎: 数据库引擎是用于存储.处理和保护数据的核心服务 Mysql支持的引擎一般有这几种: MyISAM Mysql 5.1版本之前默认的存储引擎,仅仅支持表锁,但查询速度较Inno ...
- vue2.0实现银行卡类型种类的选择
功能效果:vue2.0实现银行卡类型种类的选择 图片.png 参考代码如下: <template> <div class="app"> <header ...
- C#string转换为Datetime
DateTime.ParseExact("0710090000", "MMddHHmmss", CultureInfo.CurrentCulture, Date ...
- angular反向代理
第一步:根目录新建 proxy.conf.json target:就是代理的服务器地址. 接口地址必须是http://localhost:8081/api开头 { "/api":{ ...
- Excel数据比对-批量数据比对
1.导出现场的Excel收费规则2.有专门的代码写的测试收费规则的工具(开发自己开发的)3.在这个工具上选择,导出的收费规则Excel,点击导出按钮(导出按钮里面有计算每一列的计费结果4.Excel里 ...