hdu 1010 Tempter of the Bone (奇偶性剪枝)
题意:有一副二维地图'S'为起点,'D'为终点,'.'是可以行走的,'X'是不能行走的。问能否只走T步从S走到D?
题解:最容易想到的就是DFS暴力搜索,,但是会超时。。。=_=。。。 所以,,要有其他方法适当的剪枝;假设当前所在的位置为(x,y),终点D的位置为(ex,ey); 那么找下规律可以发现: 当 |x-ex|+|y-ey| 为奇数时,那么不管从(x,y)以何种方式走到(ex,ey)都是花费奇数步;当为偶数时同理。 这即是所谓的奇偶性剪枝。这样剪枝就可以复杂度就变为原来暴力DFS的开根号(原来复杂度不清楚该怎么计算...=_=...)
/**
* @author Wixson
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <map>
#include <set>
const int inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double EPS=1e-;
using namespace std;
typedef long long ll;
typedef pair<int,int> P; int n,m,t;
char str[][];
int ans;
int sx,sy,ex,ey;
int book[][];
int Next[][]={{,},{,},{,-},{-,}};
//
int abs(int x)
{
return x>?x:-x;
}
void dfs(int x,int y,int cnt)
{
if(str[x][y]=='D')
{
if(cnt==t)
{
ans=;
}
return;
}
//
if(cnt==t) return;
//
int temp=abs(x-ex)+abs(y-ey);
if(temp%!=(t-cnt)%) return;
//
//printf("%d %d -- %d\n",x,y,cnt);
for(int k=;k<;k++)
{
int tx=x+Next[k][],ty=y+Next[k][];
//
if(tx<||tx>=n||ty<||ty>=m||book[tx][ty]||str[tx][ty]=='X') continue;
//
book[tx][ty]=;
dfs(tx,ty,cnt+);
book[tx][ty]=;
if(ans) return;
}
}
//
int main()
{
//freopen("input.txt","r",stdin);
while(scanf("%d%d%d",&n,&m,&t)&&(n||m||t))
{
ans=;
for(int i=;i<n;i++) scanf("%s",str[i]);
//
memset(book,,sizeof(book));
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(str[i][j]=='S') sx=i,sy=j;
if(str[i][j]=='D') ex=i,ey=j;
}
}
//
book[sx][sy]=;
dfs(sx,sy,);
//
if(ans) printf("YES\n");
else printf("NO\n");
}
return ;
}
hdu 1010 Tempter of the Bone (奇偶性剪枝)的更多相关文章
- hdu 1010 Tempter of the Bone 奇偶剪枝
如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...
- 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(深度+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 题意:就是给出了一个迷宫,小狗必须经过指定的步数到达出口,并且每个格子只能走一次. 首先先来介绍一下奇偶性 ...
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...
- 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 深搜+剪枝
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- 在vSphere Client上安装虚拟机工具VMware Tools
一.什么是虚拟机工具 VMware Tools是一套安装在虚拟机操作系统中的实用程序.VMware Tools可提高虚拟机的性能,并在 VMware产品中实现多个易于使用的功能. 尽管客户机操作系统在 ...
- UNIX环境高级编程--7
进程环境main函数: C程序总是从main函数开始执行.main函数原型是: int main(int argc, char *argv[]); 当内核执行C程序时(使用一个exe ...
- strcpy 和 memcpy自实现
都是套路,详见代码注释: #include <stdio.h> #include <assert.h> #include <iostream> using name ...
- [转]Android监听ListView里Button事件
本文转自:http://blog.csdn.net/lovediji/article/details/6753349 public View getView(int position, View co ...
- struts2.1.6存在中文乱码的bug
如题,后续版本中已解决:可以通过添加filter的方式解决.
- 笔记《精通css》第3章 盒模型,定位,浮动,清理
第3章 盒模型,定位,浮动,清理 1.盒模型用到的属性width,height,padding,border,margin 普通文档流的上下垂直margin会叠加 2.块级框 与 行内框, 利用 ...
- jsp 中包含 一个路径为变量的文件
<head> <base href="<%=basePath%>"> <% String fileroot="MyJsp.jsp ...
- JS——event
触发DOM上的某个事件时,会产生一个事件对象event,这个对象中包含着所有与事件有关的信息: 普通浏览器支持 event(传参),IE678支持 window.event(无参),兼容写法: < ...
- CSS——float
float:就是在于布局,首先要介绍的是文档流(标准流),之后是浮动布局. 文档流:元素自上而下,自左而右,块元素独占一行,行内元素在一行上显示,碰到父集元素的边框换行. 浮动布局: 1.float: ...
- 关于java 关键字enum不识别的解决办法
从别人那儿拷贝过来的myeclipse java工程,打开一看标红了一大片,仔细一看,原来是不识别enum关键字,这就有点尴尬了. 我自己重新建了一个java工程,测试了下,假如我在新建工程的时候选择 ...