hdu1010Tempter of the Bone(dfs+奇偶剪枝)
题目链接: 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+奇偶剪枝)的更多相关文章
- 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 ...
- Tempter of the Bone(dfs奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- M - Tempter of the Bone(DFS,奇偶剪枝)
M - Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...
- hdoj--1010<dfs+奇偶剪枝>
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:在n*m的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...
- 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 ...
- Tempter of the Bone(dfs+奇偶剪枝)题解
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 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 ...
- HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】
Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- [转]Spring Security学习总结一
[总结-含源码]Spring Security学习总结一(补命名空间配置) Posted on 2008-08-20 10:25 tangtb 阅读(43111) 评论(27) 编辑 收藏 所属分 ...
- CDOJ 1277 智商杯考试 每周一题 div2 二分+数学
智商杯考试 题目连接: http://acm.uestc.edu.cn/#/problem/show/1277 Description 你是一个挂科选手. 你现在正在考试,你很方. 你参加的考试叫做智 ...
- Distinctive Image Features from Scale-Invariant Keypoints(个人翻译+笔记)-介绍
Distinctive Image Features from Scale-Invariant Keypoints,这篇论文是图像识别领域SIFT算法最为经典的一篇论文,导师给布置的第一篇任务就是它. ...
- moment.js 日期包装类 (说明示例)
moment.js 日期包装类 Moment.js 1创建时间对象 moment(); ...
- [Android实例] 推荐给你们一个好用的ListView、RecyclerView适配器
https://github.com/vihuela/RecyclerViewHelpper 如果用过RecyclerView的人都知道,高度不会包裹,然后写法好像也不是很简洁,甚至点击事件不好设 置 ...
- oracle 察看用户是否被锁,解锁以及改密码
以管理员身份登陆 察看用户状态(是否被锁) select * from dba_users where username='user1' 解锁 ALTER USER user1 ACCOUNT UN ...
- UITabBarController 详解
// UITabBarController 标签视图控制 // 主要管理没有层级关系的视图控制器 // 1. ViewControllers 所有被管理的视图控制器, 都在这个数组中 // 2. se ...
- windows2012 IIS部署GeoTrust证书踩过的坑。
系统:windows2012 环境:IIS8 在阿里云上买了GeoTrust证书, 按照说明下载证书到服务器, 导入证书, 给IIS站点部署https. 阿里云部署帮助文档:https://help ...
- WebSocket 是什么原理?为什么可以实现持久连接?(转载)
本文转载自知乎,来源如下: 作者:Ovear链接:https://www.zhihu.com/question/20215561/answer/40316953来源:知乎著作权归作者所有.商业转载请联 ...
- 新公司官网项目优化实践(Vue)
入职后接手website-html和website-mobile项目,发现项目加载速度不太理想,于是结合自己之前的经验对项目做了优化.此篇文章主要记录这次优化详情. 原始项目:开发环境:website ...