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 ...
随机推荐
- Node.js intro
1. require() load module http://stackoverflow.com/questions/9901082/what-is-this-javascript-require ...
- jquery动态删除html代码
1.remove() remove()方法移除被选元素,包括所有的文本和子节点. 语法:$(selector).remove() 当我们想将元素自身移除时我们用 .remove(),同时也会移除元素内 ...
- [题解]UVa 10891 Game of Sum
在游戏的任何时刻剩余的都是1 - n中的一个连续子序列.所以可以用dp[i][j]表示在第i个数到第j个数中取数,先手的玩家得到的最大的分值.因为两个人都很聪明,所以等于自己和自己下.基本上每次就都是 ...
- 一个div,包含两个div,调整文字位置和div平均分布
网页中经常会用到,一个div下平均分布两个小的div,两个小的div,显示的内容为图片还比较好处理,显示文字则不好控制效果,今天写了一个如图效果的 html: <div class=" ...
- KinectV1+Ubuntu 14.04安装教程
前言 个人理解错误的地方还请不吝赐教,转载请标明出处,内容如有改动更新,请看原博:http://www.cnblogs.com/hitcm/ 如有任何问题,feel free to ...
- const变量初始化问题
在C++中const变量定义时必须进行初始化,否则无法通过编译. 初始化的方式有多种,可以通过字面值对其进行初始化,也可以通过变量对其初始化,或其他方式,只要能给const变量赋初值即可(当然初值应该 ...
- 分享自制的13套 JQuery Mobile 界面主题(追加4套新款)
15套整合在一起的,其中2套官方+13套自制,款款精致,方便移动开发. 字体默认为微软雅黑. 适配于 JQuery Mobile 1.4.3 下载地址:http://files.cnblogs.com ...
- C#中指针的用法
(*) unsafe 和 fixed unsafe { ]; ; i < array.Length; i++) { array[i] = i; } fixed (int* p = array) ...
- 特殊的ASCII码对应的字符
Special Characters " " " quotation mark u+0022 ISOnum p:before { content:"\0022& ...
- java导入excel时遇到的版本问题
java中读取excel文件时对不同的版本提供了不同的读取方法,这就要求我们在读取excel文件时获取excel文件的版本信息从而通过不同的版本去使用不同的读取方式, 在WorkbookFactory ...