题目链接:

pid=1010">点击打开链接

题目描写叙述:给定一个迷宫,给一个起点和一个终点。问是否能恰好经过T步到达终点?每一个格子不能反复走

解题思路:dfs+剪枝

剪枝1:奇偶剪枝,推断终点和起点的距离与T的奇偶性是否一致,假设不一致,直接剪掉

剪枝2:假设从当前到终点的至少须要的步数nt加上已经走过的步数ct大于T,即nt+ct>t剪掉

剪枝3:假设迷宫中能够走的格子小于T直接剪掉

启示:剪枝的重要性

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int n,m,t;
char g[10][10];
int sx,sy,dx,dy;
bool flag[10][10];
const int nx[]= {0,1,0,-1};
const int ny[]= {1,0,-1,0};
bool dfs(int x,int y,int ct)
{
if(x==dx&&y==dy)
{
if(t==ct)
return true;
else
return false;
}
if(abs(dx-x)+abs(dy-y)+ct<=t)
{
for(int i=0; i<4; ++i)
{
int ntx=x+nx[i];
int nty=y+ny[i];
if(ntx<=n&&ntx>=1&&nty<=m&&nty>=1&&g[ntx][nty]=='.'&&!flag[ntx][nty])
{
flag[ntx][nty]=true;
if(dfs(ntx,nty,ct+1)) return true;
flag[ntx][nty]=false;
}
}
}
return false;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&t)==3&&(n!=0||m!=0||t!=0))
{
int cut=0;
for(int i=1; i<=n; ++i)
{
scanf("%s",&g[i][1]);
for(int j=1; j<=m; ++j)
{
if(g[i][j]=='S') sx=i,sy=j;
if(g[i][j]=='D') dx=i,dy=j;
if(g[i][j]=='.') cut++;
}
}
if(abs(dx-sx)+abs(dy-sy)>t||cut<t-1||(abs(dx-sx)+abs(dy-sy))%2!=t%2)
{
printf("NO\n");
continue;
}
memset(flag,false,sizeof(flag));
flag[sx][sy]=true;
g[dx][dy]='.';
if(dfs(sx,sy,0))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

hdu1010Tempter of the Bone(dfs+奇偶剪枝)的更多相关文章

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

  2. Tempter of the Bone(dfs奇偶剪枝)

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

  3. M - Tempter of the Bone(DFS,奇偶剪枝)

    M - Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  4. HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...

  5. hdoj--1010<dfs+奇偶剪枝>

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:在n*m的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...

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

  7. Tempter of the Bone(dfs+奇偶剪枝)题解

    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. HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】

    Tempter of the Bone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. codevs 1962 马棚问题--序列型DP

    1962 马棚问题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 每天,小明和他的马外出,然后他们一边跑一边玩耍.当他们结束 ...

  2. [转]Spring 中的p标签

    spring的bean配置文件中p:代表什么 <bean id="daoTemplate" abstract="true" lazy-init=" ...

  3. java8 遍历数组的几种方式

    java8 遍历数组的几种方式 2017年04月05日 09:15:46 阅读数:4640 风格色 2017-02-11 18:41 有如下一个String数组 String[] array = {& ...

  4. 二十四种设计模式:工厂方法模式(Factory Method Pattern)

    工厂方法模式(Factory Method Pattern) 介绍定义一个用于创建对象的接口,让子类决定将哪一个类实例化.Factory Method使一个类的实例化延迟到其子类. 示例有SqlMes ...

  5. [转]SSIS ProtectionLevel 对包中敏感数据的访问控制

    本文转自:http://technet.microsoft.com/zh-cn/library/ms141747.aspx 为了保护 Integration Services 包中的数据,可以设置保护 ...

  6. Win8安装程序出现2502、2503错误解决方法

    我是在安装VMware virtualbox的时候遇到的这个问题,上网百度了一下发现这是个在win8系统上安装程序时才会遇到的. 究其原因这个问题还是由于权限问题导致的,解决方法如下: 1,ctrl+ ...

  7. tensorflow c++ API加载.pb模型文件并预测图片

    tensorflow  python创建模型,训练模型,得到.pb模型文件后,用c++ api进行预测 #include <iostream> #include <map> # ...

  8. python中子类调用父类的方法

    1子类调用父类构造方法 class Animal(object): def __init__(self): print("init Animal class~") def run( ...

  9. 【License】一张图该诉你各种License的含义?

    一张图该诉你各种License的含义:

  10. 谈谈WEB开发跟非WEB开发各自不同的关注点

    何为非WEB开发呢,个人理解就是不是用浏览器打开的应用统称为非WEB开发,抽象讲可以理解成C/S模式. WEB开发,技术人员的积累在如下几个方面: HTML + CSS + JavaScript 各种 ...