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(深度+剪枝)的更多相关文章

  1. hdu 1010 Tempter of the Bone 奇偶剪枝

      如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...

  2. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. hdu 1010 Tempter of the Bone 深搜+剪枝

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

  7. hdu 1010 Tempter of the Bone(深搜+奇偶剪枝)

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

  8. 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 ...

  9. 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 ...

随机推荐

  1. 使用jQuery,实现完美的表单异步提交

    jQuery异步提交表单 <form id="form1" method="post"> <table border="1" ...

  2. Fragment中的onKeyDown事件让Activity处理--处理特殊按键比如移动终端扫描

    一些特殊按键事件需要在Activity中处理public void onKeyDown(int keyCode, KeyEvent event){ //让Activity处理 getActivity( ...

  3. Android之如何使用JUnit进行单元测试

    转的:http://www.blogjava.net/qileilove/archive/2014/05/19/413824.html Android中如何使用JUnit进行单元测试 在我们日常开发a ...

  4. Kafka的安装和部署及测试

    1.简介 大数据分析处理平台包括数据的接入,数据的存储,数据的处理,以及后面的展示或者应用.今天我们连说一下数据的接入,数据的接入目前比较普遍的是采用kafka将前面的数据通过消息的方式,以数据流的形 ...

  5. 理解python的with语句

    Python’s with statement provides a very convenient way of dealing with the situation where you have ...

  6. LCD相关知识点

    1.LCD即液晶显示器,控制原理是控制其中的电子枪,在n行*n列的屏幕上投射不同颜色从而形成图像 2.编程步骤: ①打开LCD背光将LCD背光对应的GPIO设置为禁止上拉(GPxUP相应位写入1),选 ...

  7. Spring中的IOC\DI\AOP等概念的简单学习

    IoC(Inversion of Control,控制反转).这是spring的核心,贯穿始终, 所谓IoC,对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的关系.Spr ...

  8. Back to openGL!

    #include <iostream> #include <windows.h> #include <gl/glut.h> #include <math.h& ...

  9. Mac下用brew安装nginx

    1. nginx nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TC ...

  10. 在Ubuntu 12.4 下安装 nginx, MySQL, PHP

    LNMP是时下很流行的网站配置,我在配置蝉大师服务器的时候顺带把经验做个分享,蝉大师的网址是:http://www.ddashi.com/ 1.第一步, 跟新apt-get 输入: sudo apt- ...