题目链接:  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搜索+奇偶剪枝)的更多相关文章

  1. hdu 1010 dfs搜索

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  2. HDU 1010 Tempter of the Bone (DFS+可行性奇偶剪枝)

    <题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要 ...

  3. Tempter of the Bone HDU 1010(DFS+剪枝)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  4. HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)

    需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...

  5. hdu 1010(DFS) 骨头的诱惑

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意从S出发,问能否在时间t的时候到达终点D,X为障碍 需要注意的是要恰好在t时刻到达,而不是在t时间 ...

  6. HDU 1045 (DFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...

  7. HDU 1241 (DFS搜索+染色)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...

  8. hdu 1010 回溯加奇偶性剪枝

    普通的剪枝会超时,必须加入奇偶性剪枝. 直接上图: AC代码: #include<cstdio> #include<cstring> #include<algorithm ...

  9. hdu 1979 DFS + 字典树剪枝

    http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...

随机推荐

  1. 做网站用UTF-8还是GB2312 & 各国语言对应字符集

    经常我们打开外国网站的时候出现乱码,又或者打开很多非英语的外国网站的时候,显示的都是口口口口口的字符, WordPress程序是用的UTF-8,很多cms用的是GB2312. ● 为什么有这么多编码? ...

  2. Binary Tree Right Side View

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  3. schedule CCCallfunc CCCallfuncN CCCallfuncND

    schedule(schedule_selector(HelloWorld::step), 1.0f); void HelloWorld::step(float dt) { CCLog("d ...

  4. delphi提示“Undeclared_identifier”的缺少引用单元列表

    _Stream ADODB_TLB akTop, akLeft, akRight, akBottom Controls Application (the variable not a type) Fo ...

  5. java获取tomcat路径

    获取tomcat路径 String savePath3 = System.getProperty("catalina.home"); E:\apache-tomcat-7.0.63 ...

  6. [Android Pro] Android签名与认证详细分析之一(CERT.RSA剖析)

    转载自:http://www.thinksaas.cn/group/topic/335450/ 一.Android签名概述 我们已经知道的是:Android对每一个Apk文件都会进行签名,在Apk文件 ...

  7. Android procrank , showmap 内存分析

    (一)DDMS 的Heap Dump 1) Data Object:java object. 2) Class Object:object of type Class, e.g. what you'd ...

  8. Base Filtering Engine 拒绝访问解法

    基本筛选引擎(BFE)是一种管理防火墙和 Internet 协议安全(IPsec)策略以及实施用户模式筛选的服务.停止或禁用 BFE 服务将大大降低系统的安全.还将造成 IPsec 管理和防火墙应用程 ...

  9. 【读书笔记】读《JavaScript设计模式》之观察者模式

    一.定义 在事件驱动的环境中,比如浏览器这种持续寻求用户关注的环境中,观察者模式(又名发布者-订阅者(publisher-subscripber)模式)是一种管理人与其任务之间的关系(确切地讲,是对象 ...

  10. KVM中Linux虚拟机的硬盘添加方法

    [root@cache01 ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_roo ...