HDU 1010 (DFS搜索+奇偶剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010
题目大意:给定起点和终点,问刚好在t步时能否到达终点。
解题思路:
4个剪枝。
①dep>t剪枝
②搜到一个解后剪枝
③当前走到终点最少步数>满足条件还需要走的步数剪枝(关键)
③奇偶剪枝(关键):当前走到终点步数的奇偶性应该与满足条件还需要走的步数奇偶性一致。
其中三四两步放在一步中写:remain=abs(x-ex)+abs(y-ey)-abs(dep-t)
奇偶剪枝的原理:abs(x-ex)+abs(y-ey)为奇,则说明到达终点肯定还要走奇数步,反之偶数步。于是得和abs(dep-t)即还需走的步数奇偶性协调。
所以③④剪枝条件这么写if(remain>0||remain%2) return;
#include "cstdio"
#include "string"
#include "iostream"
#include "cstring"
using namespace std;
int n,m,t,sx,sy,ex,ey,map[][],dir[][]={-,,,,,-,,};
bool vis[][],flag;
int ABS(int x) {return x<?-x:x;}
void dfs(int x,int y,int dep)
{
vis[x][y]=true;
if(dep>t) return;
if(flag) return;
if(dep==t&&map[x][y]==) {flag=true;return;}
int remain=ABS(x-ex)+ABS(y-ey)-ABS(dep-t);
if(remain>||remain&) return;
for(int s=;s<;s++)
{
int X=x+dir[s][],Y=y+dir[s][];
if(vis[X][Y]||X<||X>n||Y<||Y>m||map[X][Y]==) continue;
dfs(X,Y,dep+);
vis[X][Y]=false;
}
}
int main()
{
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
string tt;
while(cin>>n>>m>>t&&n)
{
flag=false;
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++)
{
cin>>tt;
for(int j=;j<tt.size();j++)
{
if(tt[j]=='S') {map[i][j+]=;sx=i;sy=j+;}
if(tt[j]=='D') {map[i][j+]=;ex=i;ey=j+;}
if(tt[j]=='.') map[i][j+]=;
if(tt[j]=='X') map[i][j+]=;
}
}
dfs(sx,sy,);
if(flag) printf("YES\n");
else printf("NO\n");
}
}
| 11864555 | 2014-10-13 19:36:45 | Accepted | 1010 | 515MS | 284K | 1408B | C++ | Physcal |
HDU 1010 (DFS搜索+奇偶剪枝)的更多相关文章
- hdu 1010 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+可行性奇偶剪枝)
<题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要 ...
- Tempter of the Bone HDU 1010(DFS+剪枝)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
- HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)
需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...
- hdu 1010(DFS) 骨头的诱惑
http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意从S出发,问能否在时间t的时候到达终点D,X为障碍 需要注意的是要恰好在t时刻到达,而不是在t时间 ...
- HDU 1045 (DFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...
- HDU 1241 (DFS搜索+染色)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...
- hdu 1010 回溯加奇偶性剪枝
普通的剪枝会超时,必须加入奇偶性剪枝. 直接上图: AC代码: #include<cstdio> #include<cstring> #include<algorithm ...
- hdu 1979 DFS + 字典树剪枝
http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...
随机推荐
- hMailServer+foxmail配置局域网邮件服务器
1.下载hMailServer并安装,请参考以下网址 https://www.hmailserver.org 2.安装foxmail,官网如下: http://www.foxmail.com/ 3.配 ...
- Stanford机器学习---第三讲. 逻辑回归和过拟合问题的解决 logistic Regression & Regularization
原文:http://blog.csdn.net/abcjennifer/article/details/7716281 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...
- Java数据库ResultSet转json实现
现在有很多json相关的Java工具,如json-lib.gson等,它们可以直接把JavaBean转换成json格式. 在开发中,可能会从数据库中获取数据,希望直接转成json数组,中间不通过bea ...
- 异常:The absolute uri: http://www.springframework.org/security/tags cannot be resolved in either web.xml or the jar files deployed with this application
The absolute uri: http://www.springframework.org/security/tags cannot be resolved in either web.xml ...
- log2取整效率测试
RMQ问题中有个ST算法,当然还有个标准算法.LCA问题可以转化为带限制的RMQ(RMQ+-1)问题来解决.我们姑且认为这些问题的时间复杂度是查询$O(1)$的.但是,注意到对于RMQ(/+-1)问题 ...
- jstack命令详解
jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项"-J-d64",Windows的jstack使 ...
- Redis windows安装配置与Jedis访问数据库
一 Redis概要 Redis是一个开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.它通常被称为数据结构服务器 ...
- windows下的unix工具集:UnxUtils
参考: http://blog.csdn.net/woohello/article/details/8365639 下载: http://sourceforge.net/projects/unxuti ...
- GLSL的qualifier
uniform:从应用程序到vertex shader 到fragment shader都能使用,但是值一直不变: varying:从vertex shader到fragment shader,在fr ...
- SQL 查询CET使用领悟
用到sql的遍历循环查询,如果不考虑用CET,估计又到了自己造轮子的时代了,现在觉得sql的CET确实是个好东西,针对SQL的递归查询,很是不错的方法: with etcRecommandINfo2( ...