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 ...
随机推荐
- 卸载Mysql connect 6.9.9
我们在卸载MySQL的时候,会发现有一个名为“Connector Net X.X.X”(如:Connector Net 6.9.9)软件总是卸载不成功,下面我们来看看解决方法:1. 在C盘的目录下,有 ...
- Java——Spring介绍
spring 是一个开源框架,是为了解决企业应用程序开发. 功能如下:1.目的:解决企业应用开发的复杂性.2.功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能.3.范围:任何Ja ...
- Android基于HttpUrlConnection类的文件下载
/** * get方法的文件下载 * <p> * 特别说明 android中的progressBar是google唯一的做了处理的可以在子线程中更新UI的控件 * * @param pat ...
- ionic2 打包时报错 file-opener2
在app自动更新过程中,有用到ionic-native插件:cordova-plugin-file-openner2 添加插件后,打包时有错: FAILURE: Build failed wit ...
- 正则表达式提取String子串
最近遇到了一个字符串处理的功能,忽然发现了正则表达式的强大,深深的被她的迷人魅力所吸引,以前只是听说,今天亲眼所见,亲身经历,真的彻底折服. 言归正传:java中String类里面封装了很多字符串处理 ...
- vim之补全2(完全个人定制版)
关于补全的方面要说的的确很多, 这里选择分为两个章叙述. 如果你想学vim, 你需要有很强的耐心, 如果你想锻炼这种耐心, 你可以试着先看完我之前的文章. 好了, 下面继续我们的vim补全吧. vim ...
- 【译】x86程序员手册17-第6章保护
Chapter 6 Protection 第六章 保护 6.1 Why Protection? 为什么要保护? The purpose of the protection features of th ...
- THREE.DecalGeometry(转载)
function getDecalGeometry(position, direction){ var decalGeometry = new THREE.DecalGeometry( earthMe ...
- js的加法操作表
Number + Number -> 加法 Boolean + Number -> 加法 Boolean + Boolean -> 加法 Number + String -> ...
- 【原】PHPExcel导出Excel
1.引入相关公共库PHPExcel 2.编写公共函数 public function exportExcel($excelTitle,$data,$filename='',$column_width= ...