来源:点击打开链接

看上去数据规模很小,但是必须要剪枝,否则直接爆TLE。

通过这个题可以练习奇偶剪枝。

另外:还有一个优化方式,如果所有步数走完了门还没关,则直接返回结果"NO".

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std; int n,m,tarstep;
int tari,tarj;
int si,sj;
char map[10][10];
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int ok=0; void dfs(int si,int sj,int step)
{
int temp;
if(si>n || sj>m || si<=0 || sj<=0)
return;
if(step==tarstep && si==tari && sj==tarj)
ok=1;
if(ok==1)
return;
//奇偶剪枝
temp=(tarstep-step)-abs(si-tari)-abs(sj-tarj);
if(temp<0 || temp&1)
return;
for(int i=0;i<4;i++)
{
if(map[si+dir[i][0]][sj+dir[i][1]]!='X')
{
map[si+dir[i][0]][sj+dir[i][1]]='X';
dfs(si+dir[i][0],sj+dir[i][1],step+1);
map[si+dir[i][0]][sj+dir[i][1]]='.';
}
}
return ;
} int main()
{
while(cin>>n>>m>>tarstep)
{
if(n==0 && m==0 && tarstep==0)
break;
int wall=0;
for(int i=1;i<=n;i++) //下标从1开始
{
for(int j=1;j<=m;j++)
{
cin>>map[i][j];
if(map[i][j]=='S')
{
si=i;
sj=j;
}
else if(map[i][j]=='D')
{
tari=i;
tarj=j;
}
else if(map[i][j]=='X')
wall++;
} }
if(n*m-wall<tarstep) //剪枝,如果能走的空地走完门没开
{
cout<<"NO"<<endl;
continue;
}
ok=0;
map[si][sj]='X'; //初始化破坏掉
dfs(si,sj,0);
if(ok==0)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
} }
return 0;
}

【剪枝】HDU 1010——tempter of the bone的更多相关文章

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

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

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

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

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

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

  10. HDU 1010 Tempter of the Bone(深度+剪枝)

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

随机推荐

  1. PIL(Python Image Library)生成验证码

    # -*- coding: utf-8 -*-#导入三个模块import Image,ImageDraw,ImageFontimport randomimport math'''基本功能'''#图片宽 ...

  2. UVA 558 Wormholes

    要问是否存在一个总权重为负数的环,用dfs即可解决. time:33ms #include <cstdio> #include <cstring> #define N 3000 ...

  3. 我的第一个Spring程序

    1.程序结构 2.各个文件 ByeService.java package com.service; public class ByeService { private String name; pu ...

  4. 查看wtmp文件内容

    1./var/log/wtmp文件的作用     /var/log/wtmp也是一个二进制文件,记录每个用户的登录次数和持续时间等信息!   2.查看方法:可以用last命令输出当中内容 1 2 3 ...

  5. mapreduce源码分析总结

    一 MapReduce概述 Map/Reduce是一个用于大规模数据处理的分布式计算模型,它最初是由Google工程师设计并实现的,Google已经将它完整的MapReduce论 文公开发布了.其中对 ...

  6. Wpf配置文件属性

    public MainWindow() { InitializeComponent(); this.WindowState = Properties.Settings.Default.WindowSt ...

  7. bzoj1176 2683

    我的第一道cdq分治题清明做了一下cdq分治的几道题,感觉这个东西实在是太厉害了离线大法好!关于几个经典的非数据结构做法具体可以看xhr神犇2013年的论文应用cdq分治的前提条件是不强制在线,修改操 ...

  8. NOI2014 动物园

    3670: [Noi2014]动物园 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 174  Solved: 92[Submit][Status] D ...

  9. 定制属于自己的自动化安装的linux系统镜像

    使用软件和平台 1.基于平台:                  Vmware workstation 8.0 2.基于系统镜像:               rhel-server-5.8-i386 ...

  10. arch Linux not found device 错误解决

    使用Archlinux LiveCD mount /dev.sda1 /mnt (有boot分区的挂boot) Running mkinitcpio -p linux Running grub-mkc ...