HDU 1010 Tempter of the Bone(深度+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010
题意:就是给出了一个迷宫,小狗必须经过指定的步数到达出口,并且每个格子只能走一次。
首先先来介绍一下奇偶性剪枝:

在这道题目中,如果使用剪枝的话,可以节省不少的时间。
在这道题目中,每次dfs循环时都可以判断一下小狗当前位置与终点所相差的步数,如果不为偶数的话,说明到达不了终点,就可以退出这个循环,不必继续dfs了。
在这道题目中,由于每个格子只能经过一次,所以经过一次后,可以把该点位置改为‘X’,然后我wa了好久,后来明白每次dfs循环后得把这个点的位置重新改回‘.’。
#include<iostream>
#include<cstring>
#include<string>
using namespace std; char map[][];
int d[][] = { { , }, { -, }, { , }, { , - } };
int flag,n,m,t,dx,dy; void dfs(int x,int y,int time)
{
if (x<||x>n||y<||y>m ||flag||time>t) return; //出界
if (time == t && x==dx && y==dy)
{
flag = ;
return;
}
int s1 = x - dx;
int s2 = y - dy;
int ans = t - time - abs(s1) - abs(s2); //剪枝,如果当前剩余的所要求的步数减去小狗
if (ans<||ans%) return; //当前位置与终点的步数不为偶数的话,则结束
for (int i = ; i < ; i++)
{
if (map[x + d[i][]][y + d[i][]] != 'X')
{
map[x + d[i][]][y + d[i][]] = 'X';
dfs(x + d[i][], y + d[i][], time+);
map[x + d[i][]][y + d[i][]] = '.'; //这里必须把该点的值还原回来,不然影响后续的dfs
}
}
return;
} int main()
{
int x,y,wall;
while (cin >> n >> m >> t, n && m && t)
{
if (!m || !n || !t)
{
cout << "NO" << endl;
continue;
}
flag = ;
wall = ;
for (int i = ; i <= n;i++)
for (int j = ; j <= m; j++)
{
cin >> map[i][j];
if (map[i][j] == 'S')
{
x = i;
y = j;
}
if (map[i][j] == 'D')
{
dx = i;
dy = j;
}
if (map[i][j] == 'X')
wall++;
}
if (n*m - wall <t) //剪枝,如果所有点减去墙小于指定步数,那肯定是不行的
{
cout << "NO" << endl;
continue;
}
map[x][y] = 'X';
dfs(x,y,);
if (flag == ) cout << "YES" << endl;
else cout << "NO" << endl;
}
return ;
}
HDU 1010 Tempter of the Bone(深度+剪枝)的更多相关文章
- hdu 1010 Tempter of the Bone 奇偶剪枝
如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往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 + 奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1010 Tempter of the Bone 深搜+剪枝
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1010 Tempter of the Bone(深搜+奇偶剪枝)
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 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- RN中的onChangeText
在RN学习中,按照一本书中案例书写TextInput框, 书中给了两种写法 写法1: <TextInput style={styles.pswInputStyle} placeholder='请 ...
- Mysql数据库的一些命令_LInux
查看当前数据库的版本,因为有些命令在不同版本中 用法有可能不一样,注意 -V 是大写字母V[root@localhost ~]# mysqladmin -Vmysqladmin Ver 8.42 D ...
- Dapper学习笔记(4)-事务
Dapper中对事务的处理也非常简单,如下代码所示: private void DapperTransaction() { using (IDbConnection con = OpenConnect ...
- 关于<form>标签
<form>用于为用户输入创建HTML表单,表单用于向服务器传输数据 form是块级元素,其前后会产生折行 <form>包含: 1.input元素:(根据不同的type属性,输 ...
- 使用PHPExcel导出文件
使用PHPExcel导出文件步骤及解析: 新建一个excel表格:实例化PHPExcel类 创建sheet(内置表):createSheet()方法,创建新的sheet方法 setActiveShee ...
- Java并发编程学习笔记(三)——对象的组合
重要概念: 1.在设计线程安全类的过程中,需要包含以下三个基本要素: (1)找出构成对象状态的所有变量. (2)找出约束状态变量的不变性条件. (3)建立对象状态的并发访问管理策略. 2.
- tab标签切换
<script>$(document).ready(function(){ $(".drtitle li").click(function(m){ $(this).ad ...
- Java SCP copy local file to remote implementation
最近做的项目中,有一个小需求,需要通过SCP把本地文件copy到远程服务器.查了好多资料,最终解决方案简单快速,分享一下. 在这里,需要用到4个jar包,分别是ant-jsch.jar,ant-lau ...
- 原始感知机入门——python3实现
运用最简单的原始(对应的有对偶)感知机算法实现线性分类. 参考书目:<统计学习方法>(李航) 算法原理: 踩到的坑:以为误分类的数据只使用一次,造成分类结果很差,在train函数内加个简单 ...
- 查看外网出口IP && Traceroute
一.CentOS 查看外网出口IP 1---------------- # curl ifconfig.me 2----------------# curl icanhazip.com 二.Trace ...