题目链接:  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. ubuntu 12.04 server编译安装nginx

    tar -xvf zlib-1.2.8.tar.gz cd zlib-1.2.8 ./config make make install above is for zlib(refers http:// ...

  2. maven3 junit4 spring3 jdk8 :junit一直报错,害的我几个星期都是这个错,你妹的!

    [org.springframework.test.context.junit4.SpringJUnit4ClassRunner]SpringJUnit4ClassRunner constructor ...

  3. Selenium测试Ajax程序(转)

    上周末参加了Qclub的百度技术沙龙,听了百度的孙景卫讲了Web自动化测试,讲的非常好,然后在小组讨论时又有幸座在了一起.我们讨论的一个内容,就是Ajax应用程序比原来的非Ajax程序更不易测试,这里 ...

  4. iOS 中的Certificate,Provisioning Profile 等在code singing中用到的信息

    注册apple id 有1年多了,这些概念还是模模糊糊的,决定在这里总结一下. 请参阅官方文档 App Distribution Guide code singing的作用如下: Code signi ...

  5. sockaddr struct 类型重定义

    windows.h和winsock2.h有类型重定义我是知道的,本来就一个库来说没问题,把winsock2放到windows.h前或先定义WIN32_LEAN_AND_MEAN都能解决问题但现的出了问 ...

  6. Java for LeetCode 028 Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  7. codeforces 485A.Factory 解题报告

    题目链接:http://codeforces.com/problemset/problem/485/A 题目意思:给出 a 和 m,a 表示第一日的details,要求该日结束时要多生产 a mod ...

  8. hdu 1098 Lowest Bit 解题报告

    题目链接:http://code.hdu.edu.cn/game/entry/problem/show.php?chapterid=1&sectionid=2&problemid=22 ...

  9. Spring MVC程序中得到静态资源文件css,js,图片

    转载自:http://www.blogjava.net/fiele/archive/2014/08/24/417283.html 用 Spring MVC 开发应用程序,对于初学者有一个很头疼的问题, ...

  10. Linux下的ip命令,除了ifconfig还有很多

    linux的ip命令和ifconfig类似,但前者功能更强大,并旨在取代后者.使用ip命令,只需一个命令,你就能很轻松地执行一些网络管理任务.ifconfig是net-tools中已被废弃使用的一个命 ...