DFS Tempter of the Bone
http://acm.hdu.edu.cn/showproblem.php?pid=1010
用到了奇偶剪枝:
0 1 0 1
1 0 1 0 如图,设起点为s,终点为e,s->e的最短步数为t. sum=t+extra. [因为extra无论怎么走,最终都要返回其中一条最短路径上,即有来回,所以extra=2*某个数,一定是偶数。
0 1 0 1
1 0 1 0
【所以此题中,v-step表示还可以走多少步,abs(x-x2)+abs(y-y2)为此刻点到终点位置,由上面剪枝分析,两者之差一定为偶数。所以根据小学数学,奇-奇=偶,偶-偶=偶,可知两数奇偶性相同(有用吗)】
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
int fa[][]={{,},{,-},{,},{-,}};
int n,m,v;
char mo[][];
int mark[][];
int ans,x1,x2,y3,y2;
int coun;
int abs(int a)
{
if (a<) return -a;
return a;
}
void dfs(int x,int y,int step)
{
if(ans) return ;
if(x==x2&&y==y2&&step==v) {ans=;return ;}
int v1=v-step-(abs(x-x2)+abs(y-y2));
if(v1<||v1&)
return ;
for(int i=;i<;i++)
{
int xx=x+fa[i][];
int yy=y+fa[i][];
if(xx>=&&xx<n&&yy>=&&yy<m&&mark[xx][yy]==&&mo[xx][yy]!='X')
{
mark[xx][yy]=;
dfs(xx,yy,step+);
if(ans) return ;
mark[xx][yy]=;
}
}
return ;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&v)&&(n||m||v))
{
ans=coun=;
mem(mark);
for(int i=;i<n;i++)
scanf("%s",mo[i]);
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(mo[i][j]=='S')
{x1=i;y3=j;}
if(mo[i][j]=='D')
{x2=i;y2=j;}
if(mo[i][j]=='X')
coun++;
}
}
if(v>n*m-coun-)
{
printf("NO\n");
continue;
}
mark[x1][y3]=;
dfs(x1,y3,);
if(ans==)printf("YES\n");
else printf("NO\n");
}
return ;
}
DFS Tempter of the Bone的更多相关文章
- DFS:Tempter of the Bone (规定时间达到规定地点)
解题心得: 1.注意审题,此题是在规定的时间达到规定的地点,不能早到也不能晚到.并不是最简单的dfs 2.在规定时间达到规定的地点有几个剪枝: 一.公式:所需的步骤 - 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(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
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- hdoj 1010 Tempter of the Bone【dfs查找能否在规定步数时从起点到达终点】【奇偶剪枝】
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Tempter of the Bone(dfs+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Tempter of the Bone(dfs奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)
题意 一仅仅狗要逃离迷宫 能够往上下左右4个方向走 每走一步耗时1s 每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次 问狗是否有可能逃离这个迷宫 直接DFS 直道找到满足条件的路径 ...
- 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 ...
随机推荐
- springmvc框架原理分析和简单入门程序
一.什么是springmvc? 我们知道三层架构的思想,并且如果你知道ssh的话,就会更加透彻的理解这个思想,struts2在web层,spring在中间控制,hibernate在dao层与数据库打交 ...
- oracle 11g完全彻底的卸载
1.关闭oracle所有的服务.可以在windows的服务管理器中关闭: 2.打开注册表:regedit 打开路径: <找注册表 :开始->运行->regedit> HKEY ...
- Confluence 6 管理员联系表单的后台配置界面
管理员联系表单的后台配置界面截图和配置. 对输入的数据进行编辑和选择是否启用发送电子邮件给管理员 https://www.cwiki.us/display/CONFLUENCEWIKI/Configu ...
- Confluence 6 复杂授权或性能问题
提交一个 服务器请求(support request) 然后在你的服务请求中同时提供下面的信息. Confluence 服务器 登录 Confluence 然后访问管理员控制台. 将 系统信息(Sys ...
- vue this触发事件
@click="aHref(index,$event)" aHref: function(url,event){ this.$router.push(url); $(event.c ...
- LeetCode(99):恢复二叉搜索树
Hard! 题目描述: 二叉搜索树中的两个节点被错误地交换. 请在不改变其结构的情况下,恢复这棵树. 示例 1: 输入: [1,3,null,null,2] 1 / 3 \ 2 输出 ...
- PDF如何去除背景,PDF去除背景颜色
PDF文件在使用的时候大多都是单调的白色背景,但是也有小伙伴再制作PDF文件的时候会给PDF文件添加背景颜色,会有影响文字阅读的情况,这个时候就需要把背景颜色去除了,那么该怎么做呢,不会的小伙们就跟小 ...
- linux 基础知识(三)
抽空把Linux的一些基础的东西再补充一下,安全的东西真的很多都是要自己不断的学习,很多还是今天学习了一点时间过后不用就会忘记.所以学习的东西就是要不断地往复. 有时候感觉有时候快就是慢,慢就是快. ...
- Friends number
问题 : Friends number 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Paula and Tai are couple. There are many stories ...
- Linux下source命令详解
source命令用法 source FileName source命令作用 在当前bash环境下读取并执行FileName中的命令. *注:该命令通常用命令“.”来替代. 使用范例: source f ...