HDU 1010 Tempter of the Bone
题意:从开始位置走到结束位置,恰好走 t 步 YES
否则 NO
搜索题,由于是恰好走到,所以用到了奇偶剪枝
什么是奇偶剪枝,我也是刚知道
所给步数为 t ,起始位置坐标 (begin_x,begin_y), 结束位置坐标 (end_x,end_y)
两位置最短距离为 ju = abs(end_x - begin_x) + abs(end_y - begin_y)
若 t - ju 为奇数,则无论如何不能恰好走到
为偶数才有可能恰好走到
代码中 pos + dis 即 ju
代码如下:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
int m,n,t;
char map[][];
int vis[][];
int flag;
int begin_x,begin_y,end_x,end_y;
int p[][] = {{-,},{,},{,},{,-}}; //偏移量:上,下,左,右
void dfs(int x,int y,int pos) //pos 为当前已走步数
{
if(flag) return ; //剪枝:已经到达目的地
if(pos > t) return ; //剪枝:步数超过 t
if(x == end_x && y == end_y && pos == t) //恰好 t 步走到
{
flag = ;
return ;
}
int dis = abs(end_x - x) + abs(end_y - y); //当前位置到结束位置的最短距离
if((t - dis - pos) % != ) return; //奇偶剪枝,很重要,不剪TLE
for(int i = ; i < ; i ++) //搜索四个方向
{
int tx = x + p[i][];
int ty = y + p[i][];
if(tx >= && tx < m && ty >= && ty < n && !vis[tx][ty] && map[tx][ty] != 'X')
{
vis[tx][ty] = ;
dfs(tx,ty,pos + );
vis[tx][ty] = ;
}
}
}
int main()
{
while(~scanf("%d%d%d",&m,&n,&t) && (m || n || t))
{
memset(vis,,sizeof(vis));
flag = ;
int k = ;
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j++)
{
cin>>map[i][j]; //scanf注意回车
if(map[i][j] == 'S')
{
begin_x = i;
begin_y = j;
vis[i][j] = ;
}
if(map[i][j] == 'D')
{
end_x = i;
end_y = j;
}
if(map[i][j] == 'X')
k ++;
}
}
if((m * n - k) > t) dfs(begin_x,begin_y,);
if(flag) printf("YES\n");
else printf("NO\n");
}
return ;
}
HDU 1010 Tempter of the Bone的更多相关文章
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- 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 ...
- hdu 1010 Tempter of the Bone 奇偶剪枝
如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...
- 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 ...
- 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 ...
- 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 ...
- hdu 1010 Tempter of the Bone 深搜+剪枝
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 ...
- hdu 1010 Tempter of the Bone(深搜+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 题解报告:hdu 1010 Tempter of the Bone
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Problem Description The doggie found a bone in a ...
随机推荐
- 处理sevenzipsharp 检查密码函数的Bug
using (SevenZipExtractor extr = new SevenZipExtractor(tbPackagePath.Text, "www.pc6.com")) ...
- sql server 查询和Kill死锁进程
查询死锁进程语句 select request_session_id spid, OBJECT_NAME(resource_associated_entity_id) tab ...
- LDAP与Samba
默认的Samba服务器支持本地系统用户(smbpasswd添加后)访问Samba资源,不支持OpenLDAP服务器账号访问Samba共享资源 目的:配置完后,OpenLDAP每新增一个用户,就自动支持 ...
- CentOS6.5 vsftpd 配置
CentOS6.5vsftpd 配置文件为/etc/vsftpd/vsftpd.conf 安装完软件后:1.默认匿名用户能够登陆,且限制在/pub目录内,2.本地用户可以登陆但因SElinux而无法登 ...
- Redis的5种数据结构
Redis可以存储可以存储键与5种不同数据结构类型之间的映射. 五种结构类型为:STRING(字符串).LIST(列表).SET(集合).HASH(散列).ZSET(有序集合). 1.字符串类型Str ...
- SQL Server 列存储性能调优(翻译)
原文地址:http://social.technet.microsoft.com/wiki/contents/articles/4995.sql-server-columnstore-performa ...
- 消除a标签点击后产生的虚线框
为a标签添加这条属性: a:focus {outline:none;-moz-outline:none;}
- .NET平台开发Mongo基础知识
NoSQL简介 NoSQL相关的技术最近越来越受欢迎,Mongo本身就是基于NoSQL实现的.关于NoSQL你需要了解 什么是NoSQL NoSQL和传统的关系型数据库有什么区别 NoSQL的优缺点 ...
- SQL 列转行的实现
--列转行,逗号拼接指定列的值Oracle中写法:select wmsys.wm_concat(Field1) from TableASQL Server中写法:SELECT STUFF(( SELE ...
- IIS 7 Web服务器上部署ASP.NET网站(转)
IIS 7 Web服务器上部署ASP.NET网站小记 摘自:http://swanmsg.blog.sohu.com/162111073.html 网上查找了很久关于iis7配置asp.net配置问题 ...