UVALive 7297 Hounded by Indecision BFS
题意:map中给出小偷的位置,警察的位置。警察有一只狗,开始的时候警察和狗一起行动,也就是看做一个格子,当警察遇见小偷走过的格子时,狗就会嗅到它的气味,以2倍的速度追捕小偷。现在,小偷希望知道自己是否有安全路线逃出去,以此决定是否要继续拿着偷的东西。出口不止一处,只有一个警察和一只狗,一个小偷。【知道也才不告诉他,哼。】
思路:因为小偷每走到一个格子时,时间为t1,决定他是否被抓到的因素是警察到这里的时间t2。当t1>=t2时,小偷被抓,当t1<t2时,小偷还能从该点继续走,直到t2-t1+t2时刻被狗抓住或者跑出去。所以首先对警察bfs,确定警察到map上的每个格子最短时间t2。然后,对小偷bfs,判断当前点是否能走的条件:map范围内且不是围墙 && 到达时间t1<t2 && 还没有到限制的时间 && (没有走过 || 之前走到当前路线的时间比现在小)。注意:我们每到一个新的点,当前点的限制是前一个点的限制和当前点计算得到的限制的max值,而我们可以选择一条路线使得再次到达当前点的步数更小,获得的逃跑时间更长。
【事实证明:"之前走到当前路线的时间比现在小"条件可以去掉,因为本题中 任何位置bfs第一次搜索到的时间,一定是最短时间。】
代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#define maxn 210
#define inf 0x7f7f7f7f
using namespace std; int step[maxn][maxn];
char mp[maxn][maxn];
int vis[maxn][maxn]; // lim
int dir[4][2] = {1,0, -1,0, 0,1, 0,-1};
int w, h; struct Node {
int x, y, lim, step;
Node () {
lim = inf;
step = inf;
}
}q[maxn*maxn*maxn], st; struct Point{
int x, y;
}que[maxn*maxn*maxn], now, nxt, temp; bool check(int x, int y) {
if (x>=0 && x<h && y>=0 && y<w && mp[x][y] != 'X')
return true;
return false;
} void bfs_police(int px, int py){
memset(vis, 0, sizeof(vis));
vis[px][py] = 1;
step[px][py] = 0;
now.x = px, now.y = py;
int head = 0, tail = -1;
que[++tail] = now;
while(head <= tail) {
now = que[head++];
for (int i=0; i<4; ++i) {
nxt.x = now.x + dir[i][0];
nxt.y = now.y + dir[i][1];
if (check(nxt.x, nxt.y) && vis[nxt.x][nxt.y] == 0) {
step[nxt.x][nxt.y] = step[now.x][now.y] + 1;
vis[nxt.x][nxt.y] = 1;
que[++tail] = nxt;
}
}
}
} bool bfs_thief(Node st) {
memset(vis, 0, sizeof(vis));
st.step = 0;
st.lim = (step[st.x][st.y] - st.step) + step[st.x][st.y];
int head = 0, tail = -1;
q[++tail] = st;
vis[st.x][st.y] = st.lim; while(head <= tail) {
Node now = q[head++];
if (mp[now.x][now.y] == 'E') {
return true;
}
for (int i=0; i<4; ++i) {
Node nxt = now;
nxt.x += dir[i][0];
nxt.y += dir[i][1];
nxt.step++;
if (check(nxt.x, nxt.y)) {
int lim = step[nxt.x][nxt.y] + (step[nxt.x][nxt.y] - nxt.step);
if (lim < nxt.lim) nxt.lim = lim;
if (nxt.step < step[nxt.x][nxt.y] && nxt.step < nxt.lim && (vis[nxt.x][nxt.y] == 0 || nxt.lim > vis[nxt.x][nxt.y])) {
vis[nxt.x][nxt.y] = nxt.lim;
q[++tail] = nxt;
}
}
}
}
return false;
} int main() {
//freopen("in.cpp", "r", stdin);
while(~scanf("%d%d", &w, &h)) {
if (w<3 || h<3) break;
getchar();
int px, py;
for (int i=0; i<h; ++i) {
gets(mp[i]);
int len = strlen(mp[i]);
for (int j=0; j<len; ++j) {
if (mp[i][j] == 'K') {
px = i, py = j;
}
else if (mp[i][j] == 'T') {
st.x = i, st.y = j;
}
}
}
bfs_police(px, py);
bool esc = bfs_thief(st);
if (esc) printf("KEEP IT\n");
else printf("DROP IT\n");
}
return 0;
}
UVALive 7297 Hounded by Indecision BFS的更多相关文章
- UVALive 7297 bfs
题意 一个小偷偷到了项链 他想知道自己是否可以逃出去 地图中有一个小偷 一个警察 警察有一条狗 一开始 小偷和警察的移动速度都是1 当警察走到小偷经过过的地方时 警察会有一条狗嗅到小偷的气味并且以2的 ...
- UVALive 6665 Dragonâs Cruller --BFS,类八数码问题
题意大概就是八数码问题,只不过把空格的移动方式改变了:空格能够向前或向后移动一格或三格(循环的). 分析:其实跟八数码问题差不多,用康托展开记录状态,bfs即可. 代码: #include <i ...
- UVALive 3956 Key Task (bfs+状态压缩)
Key Task 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/D Description The Czech Technica ...
- UVALive 2520 Holedox Moving(BFS+状态压缩)
这个题目在比赛的时候我们是没有做出来的,但是听到他们说进制哈希的时候,感觉真的是挺高端的,于是赛后开始补题,本着我的习惯在看题解之前自己再试着写一遍,我当时存储状态的方法是string + map,我 ...
- UVALive 2035 The Monocycle(BFS状态处理+优先队列)
这道题目真是非常坎坷啊,WA了很多次,但所有的思路都是奔着广搜去想的,一开始出现了比答案大的数据,才想到了应该是优先队列,再说加上也肯定不会错.一开始我读错了题意,以为旋转并且前行需要的时间跟其他一样 ...
- UVALive 6485 Electric Car Rally (BFS,PQ)
https://icpcarchive.ecs.baylor.edu/index.php? option=com_onlinejudge&Itemid=8&page=show_prob ...
- UVALive-7297-Hounded by Indecision
OK, maybe stealing the Duchess’s favorite ruby necklace was not such a good idea. You were makingyou ...
- What a Ridiculous Election UVALive - 7672 (BFS)
题目链接: E - What a Ridiculous Election UVALive - 7672 题目大意: 12345 可以经过若干次操作转换为其它五位数. 操作分三种,分别为: 操作1:交 ...
- UVALive 5066 Fire Drill BFS+背包
H - Fire Drill Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Sta ...
随机推荐
- 转 cocos2dx内存优化
cocos2dx里面,sprite本身不消耗多少内存,只是关联的材质文件消耗内存.假设有10个sprite关联同一个材质,也不会有10倍消耗. 关于图片占用的材质内存,我觉得还有好几种优化手段:1.对 ...
- iOS - Swift 与 Objective-C 互相操作
前言 在 Swift 语言中,我们可以使用 Objective-C.C 语言编写代码,我们可以导入任意用 Objective-C 写的 Cocoa 平台框架.Objective-C 框架或 C 类库. ...
- 流媒体基础实践之——Nginx-RTMP-module 指令详解
转载网址:http://blog.csdn.net/aoshilang2249/article/details/51483814
- 小度Wifi_设置
PS:现在我用的小度Wifi驱动的 安装程序的版本为:“XiaoduWiFi140923_M_3.0.9.rar”(保存于“百度云 OsSkill --> 软件安装包 > 小度Wifi__ ...
- mysql 理解 int(11)
1.这里的int(11) 与int的大小和存储字节,没有一毛钱关系,int的存储字节是4个字节,最大值为 65536*65536 = 40多亿,对于有符号的int,是20多亿.2.那么这里的(11) ...
- hdu 1081(最大子矩阵和)
题目很简单,就是个最大子矩阵和的裸题,看来算法课本的分析后也差不多会做了.利用最大子段和的O(n)算法,对矩阵的行(或列)进行 i和j的枚举,对于第 i到j行,把同一列的元素进行压缩,得到一整行的一维 ...
- overflow:auto/hidden的应用
一.自适应两栏布局 <!DOCTYPE html><html lang="zh-CN"><head> <meta charset=&quo ...
- golang 定时器
上网查了下相关资料,基本上都介绍的是github.com\robfig\cron这个包来执行定时任务,试了下确实可以执行.但是此包下没有删 除任务的方法,只有暂停的方法(Stop),若要停止之前的任务 ...
- Java中去除StringBuffer最后一个字符
原文:http://www.cnblogs.com/shaozhiheng/p/3661714.html 由于编写了这么一段代码: Iterator it3 = set.iterator(); whi ...
- 转!数据库连接池概念、种类、配置(DBCP\C3P0\JndI与Tomact配置连接池)
数据库连接池概念.种类.配置(DBCP\C3P0\JndI与Tomact配置连接池) 一.DBCP 连接:DBCP 连接池是 Apache 软件基金组织下的一个开源连接池实现. 需要的 java 包c ...