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 ...
随机推荐
- 如何下载google play免费应用的apk文件
到这里: http://apps.evozi.com/apk-downloader/ 一看便知.
- ios抓包官方文档
OS X Programs OS X supports a wide range of packet trace programs, as described in the following sec ...
- SQL表值函数和标量值函数的区别
SQL表值函数和标量值函数的区别 写sql存储过程经常需要调用一些函数来使处理过程更加合理,也可以使函数复用性更强,不过在写sql函数的时候可能会发现,有些函数是在表值函数下写的有些是在标量值下写的, ...
- jquery check box
if ($("#eulaLine").is(':checked')) { var mobile = $("#mobile").val(); if (mobile ...
- soem函数库的编译
D:/并条机/soem/soem-master/doc/html/files.htm https://github.com/smits/soem https://github.com/OpenEthe ...
- NanoApe Loves Sequence-待解决
NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java ...
- ShortestPath:Layout(POJ 3169)(差分约束的应用)
布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...
- CodeForces - 426B(对称图形)
Sereja and Mirroring Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64 ...
- Docker跨主机通信之路由
一.实验环境: 主机名 主机IP Docker0_IP Docker1 192.168.88.130 172.17.0.1 Docker2 192.168.88.131 172.18.0.1 二.实验 ...
- phpstorm 8 license key
Learn Programming===== LICENSE BEGIN =====63758-1204201000000Ryqh0NCC73lpRm!XVcxFChJ2gTUR2lZtlLXrPLb ...