HDU 1010 Tempter of the Bone (DFS+剪枝)
题意:从S走到D,能不能恰好用T时间。
析:这个题时间是恰好,并不是少于T,所以用DFS来做,然后要剪枝,不然会TEL,我们这样剪枝,假设我们在(x,y),终点是(ex,ey),
那么从(x, y)到(ex, ey),要么时间正好是T-你已经走过的时间,要么要向别的地方先拐一下,以凑出这个正好时间,既然要拐一下,那么一定要回来,
所以时间肯定得是偶数,要不然完不成(回不来),
所以(t - abs(ex-x) - abs(ey-y) - cnt ),如果是奇数就剪枝。然而用C++交就TLE,用G++就AC。。。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e4 + 5;
const int mod = 1e9 + 7;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int t;
char s[10][10];
int vis[10][10];
int sx, sy, ex, ey; bool dfs(int r, int c, int cnt){
if(cnt > t) return false;
if(r == ex && c == ey && cnt == t) return true;
if((t - abs(ex-r) - abs(ey-c) - cnt) & 1) return false; for(int i = 0; i < 4; ++i){
int x = dr[i] + r;
int y = dc[i] + c;
if(!is_in(x, y) || vis[x][y] || s[x][y] == 'X') continue;
vis[x][y] = 1;
if(x == ex && ey == y && cnt + 1 == t) return true;
if((t - abs(ex-x) - abs(ey-y) - cnt -1) & 1) continue;
if(dfs(x, y, cnt+1)) return true;
vis[x][y] = 0;
}
return false;
} int main(){
while(scanf("%d %d %d", &n, &m, & t) == 3){
if(!n && !m && !t) break;
for(int i = 0; i < n; ++i)
scanf("%s", s[i]);
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
if(s[i][j] == 'S') sx = i, sy = j;
else if(s[i][j] == 'D') ex = i, ey = j;
memset(vis, 0, sizeof(vis));
vis[sx][sy] = 1;
if(dfs(sx, sy, 0)) puts("YES");
else puts("NO");
}
return 0;
}
HDU 1010 Tempter of the Bone (DFS+剪枝)的更多相关文章
- 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)必须是二的倍数. 关于第二种方案的解释 ...
- (step4.3.1) hdu 1010(Tempter of the Bone——DFS)
题目大意:输入三个整数N,M,T.在接下来的N行.M列会有一系列的字符.其中S表示起点,D表示终点. .表示路 . X表示墙...问狗能有在T秒时到达D.如果能输出YES, 否则输出NO 解题思路:D ...
- 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+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...
- HDU 1010 Tempter of the Bone DFS(奇偶剪枝优化)
需要剪枝否则会超时,然后就是基本的深搜了 #include<cstdio> #include<stdio.h> #include<cstdlib> #include ...
- HDU 1010 Tempter of the Bone (DFS+可行性奇偶剪枝)
<题目链接> 题目大意:一个迷宫,给定一个起点和终点,以及一些障碍物,所有的点走过一次后就不能再走(该点会下陷).现在问你,是否能从起点在时间恰好为t的时候走到终点. 解题分析:本题恰好要 ...
- HDOJ.1010 Tempter of the Bone (DFS)
Tempter of the Bone [从零开始DFS(1)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tem ...
- 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 ...
随机推荐
- Codeforces Round #206 (Div. 1)B(记忆化)
这题刚开始理解错题意了 以为只能往右和下走 这题挺好的 看题解看了N久啊 二维的DP 第一维表示走到第几步 可以画一个正方形 以左上角斜着划线 第i步走的点只能是第i条线上的点 而dp的第二维 就表示 ...
- Test语言编译器V0.8
感觉这个挺好耍的,书上的代码有错误,而且功能有限. 一.词法分析 特点: (1)可对中文进行识别:(2)暂不支持负数,可以在读入‘-'时进行简单标记后就能对简单负数进行识别了. #include &l ...
- Error: "DEVELOPER_DIR" is not defined at ./symbolicatecrash line 53
项目问题解析“Error: "DEVELOPER_DIR" is not defined at ./symbolicatecrash line 53.”这个问题是最近调试app的时 ...
- 任E行M1端口比特率特征码
分辨率:800x480gps端口:com1比特率:4800设备特征码:01D1D008内存:128M
- javascript 命名空间的实例应用
/** * 创建全局对象MYAPP * @module MYAPP * @title MYAPP Global */ var MYAPP = MYAPP || {}; /** * 返回指定的命名空间, ...
- Google的通用翻译机能成为未来的巴别鱼吗?
“巴别鱼,”<银河系漫游指南>轻轻朗读着,“体型很小,黄色,外形像水蛭,很可能是宇宙中最奇异的事物.它靠接收脑电波的能量为生,并且不是从其携带者身上接收,而是从周围的人身上.……如果你把一 ...
- PHP的GD库函数大全
GetImageSize作用:取得图片的大小[即长与宽] 用法:array GetImageSize(string filename, array [imageinfo]); ImageArc作用: ...
- HDU 5783 Divide the Sequence
Divide the Sequence Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- Hilbert先生旅馆的故事
以前上实变函数的时候稍微讲了下这个故事呢. 来自Hansschwarzkopf 很久很久以前,在欧洲某国的一个小镇上,Hilbert先生开了一家拥有无数个房间的旅馆.一天,旅馆生意红火得一塌糊涂,不到 ...
- MultiSet
Guava引进了JDK里没有的,但是非常有用的一些新的集合类型.所有这些新集合类型都能和JDK里的集合平滑集成.Guava集合非常精准地实现了JDK定义的接口.Guava中定义的新集合有: Multi ...