题目链接:

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. java中interface的完整表述

    我用一个工具:java Decompiler反编译工具查看jar包源码的时候,出现了以下代码: public abstract interface AbsITest{} 在网上搜索了一下: 我对这种情 ...

  2. [Apache手册]Linux环境下配置Apache运行cgi

    CGI动态页面 相关模块 相关指令 mod_alias mod_cgi AddHandler Options ScriptAlias CGI(公共网关接口)定义了web服务器与外部内容生成程序之间交互 ...

  3. Inno Setup入门(十八)——Inno Setup类参考(4)

    http://379910987.blog.163.com/blog/static/3352379720112122533866/ 编辑框 编辑框也叫文本框,是典型的窗口可视化组件,既可以用来输入文本 ...

  4. error C2248: 'MyString::pCharArray' : cannot access private member declared in class 'MyString'

    std::ostream & operator<<(std::ostream os,const MyString & mystr){os<<mystr.pCha ...

  5. 剖析ASP.NET Core MVC(Part 1)- AddMvcCore(译)

    原文:https://www.stevejgordon.co.uk/asp-net-core-mvc-anatomy-addmvccore发布于:2017年3月环境:ASP.NET Core 1.1 ...

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

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

  7. python中在ubuntu中安装虚拟环境及环境配置

    python中在ubuntu中安装虚拟环境及环境配置 1.升级python包管理工具pip pip install --upgrade pip 备注:当你想升级一个包的时候 `pip install ...

  8. Razor - 模板引擎 / 代码生成 - RazorEngine

    目录 Brief Authors Official Website RazorEngine 的原理 - 官方解释 安装记录 Supported Syntax (默认实现支持的语法) 测试记录 - ca ...

  9. jTemplates模板学习笔记

    1.jTemplates工作方式   1)setTemplateElement:指定可处理的模板对象 2)processTemplate:对模板化的对象进行数据处理 2.语法解析   1)jTempl ...

  10. Eclipse通过Spket增加JQuery提示的方法

    Eclipse通过Spket增加JQuery提示的方法 1.增加在线更新源:Help->Install New Software...->Add...->Name: "Sp ...