【剪枝】HDU 1010——tempter of the bone
来源:点击打开链接
看上去数据规模很小,但是必须要剪枝,否则直接爆TLE。
通过这个题可以练习奇偶剪枝。
另外:还有一个优化方式,如果所有步数走完了门还没关,则直接返回结果"NO".
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std; int n,m,tarstep;
int tari,tarj;
int si,sj;
char map[10][10];
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int ok=0; void dfs(int si,int sj,int step)
{
int temp;
if(si>n || sj>m || si<=0 || sj<=0)
return;
if(step==tarstep && si==tari && sj==tarj)
ok=1;
if(ok==1)
return;
//奇偶剪枝
temp=(tarstep-step)-abs(si-tari)-abs(sj-tarj);
if(temp<0 || temp&1)
return;
for(int i=0;i<4;i++)
{
if(map[si+dir[i][0]][sj+dir[i][1]]!='X')
{
map[si+dir[i][0]][sj+dir[i][1]]='X';
dfs(si+dir[i][0],sj+dir[i][1],step+1);
map[si+dir[i][0]][sj+dir[i][1]]='.';
}
}
return ;
} int main()
{
while(cin>>n>>m>>tarstep)
{
if(n==0 && m==0 && tarstep==0)
break;
int wall=0;
for(int i=1;i<=n;i++) //下标从1开始
{
for(int j=1;j<=m;j++)
{
cin>>map[i][j];
if(map[i][j]=='S')
{
si=i;
sj=j;
}
else if(map[i][j]=='D')
{
tari=i;
tarj=j;
}
else if(map[i][j]=='X')
wall++;
} }
if(n*m-wall<tarstep) //剪枝,如果能走的空地走完门没开
{
cout<<"NO"<<endl;
continue;
}
ok=0;
map[si][sj]='X'; //初始化破坏掉
dfs(si,sj,0);
if(ok==0)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
} }
return 0;
}
【剪枝】HDU 1010——tempter of the bone的更多相关文章
- 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 深搜+剪枝
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 分类: 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(深度+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 题意:就是给出了一个迷宫,小狗必须经过指定的步数到达出口,并且每个格子只能走一次. 首先先来介绍一下奇偶性 ...
随机推荐
- AOT
预 (AOT) 编译器 https://angular.cn/docs/ts/latest/cookbook/aot-compiler.html To run your app in AoT mode ...
- 对C语言中sizeof细节的三点分析
转自对C语言中sizeof细节的三点分析 1.sizeof是运算符,跟加减乘除的性质其实是一样的,在编译的时候进行执行,而不是在运行时才执行. 那么如果编程中验证这一点呢?ps:这是前两天朋友淘宝面试 ...
- unity 引用 移动mm 支付sdk
原地址:http://blog.csdn.net/u012085988/article/details/17531005 资源下载:http://download.csdn.net/detail/u0 ...
- RubyCritic:一款不错的检测代码质量工具
关注代码质量是高效开发必须要做的一件事,那么在 Ruby 开发的过程中,是否有什么好的代码质量检测工具呢?下面由 Ruby 工程师路英瑞介绍一下 RubyCritic--一款还不错的代码质量检测工具. ...
- 聊聊 Statsd 和 Collectd 那点事!
StatsD 是由 Etsy 开发并发布的汇总和总结应用指标的一个简单的守护进程,近些年来发展迅速,已经变成了一个用于收集应用性能指标的统一的协议. 关于 Statsd 的使用已经有很多文章介绍过,所 ...
- 这个帖子要收藏,以后用得着--python 实时获取子进程输出
在论坛上找到方法了,http://bbs.csdn.net/topics/340234292 http://blog.csdn.net/junshao90/article/details/821575 ...
- 选择排序SelectSort
/** * * @author Administrator * 功能:选择排序法 */ package com.test1; import java.util.Calendar; public cla ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- 推荐GitHub上10 个开源深度学习框架
推荐GitHub上10 个开源深度学习框架 日前,Google 开源了 TensorFlow(GitHub),此举在深度学习领域影响巨大,因为 Google 在人工智能领域的研发成绩斐然,有着雄厚 ...
- poj3254Corn Fields(状压)
http://poj.org/problem?id=3254 第一个状压题 思路挺好想 用二进制表示每行的状态 然后递推 用左移 右移来判断是不是01间隔出现 c大神教的 我的代码WA在这个地方了.. ...