来源:点击打开链接

看上去数据规模很小,但是必须要剪枝,否则直接爆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. jQuery.Validate自定义规程的使用案例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Codeforces Round #237 (Div. 2)

    链接 A. Valera and X time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inp ...

  3. asp 下拉框二级联动

    <script language = "JavaScript"> //js开始 var aaa;//定义aaa变量 aaa=0;//aaa赋0 bb = new Arr ...

  4. strspn和strcspn妙用

    http://blog.csdn.net/aidenliu/article/details/5460201

  5. Universal Asynchronous Receiver/Transmitter

    USART簡介與特性 NRZ標準資料格式(Mark/Space) 半雙工/全雙工 Synchronous 同步傳輸 CLOCK SKEW Asynchronous 非同步傳輸 半/全雙工.同步/非同步 ...

  6. Shell编程学习---第五篇:Shell的输入和输出

    在shell脚本中,可以用几种不同的方式读入数据:可以使用标准输入—缺省为键盘,或 者指定一个文件作为输入.对于输出也是一样:如果不指定某个文件作为输出,标准输出总 是和终端屏幕相关联.如果所使用命令 ...

  7. Oracle 常用符号CHR

    select  chr(92)||chr(102) from dual; \f select  chr(92)||chr(110) from dual; \n select  chr(92)||chr ...

  8. hadoop异常:java.lang.RuntimeException: java.lang.NoSuchMethodException

    出现异常的程序大致框架是这样的: public class getMaxTemperature extends Configured implements Tool { ... class MaxTe ...

  9. 用if else 判断是不是7的倍数等

    static void Main(string[] args)        {            while (true)            {                int b; ...

  10. BASE64编码规则及C#实现

    一.编码规则      Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码.它将需要编码的数据拆分成字节数组.以3个字节为一组.按顺序排列24位数据,再把这24位数据分成4组 ...