OK, maybe stealing the Duchess’s favorite ruby necklace was not such a good idea. You were making
your way toward the city gates when you heard the sound you had been dreading: a sharp whistle
followed by an answering bark. You know that the constable has just fetched his favorite hound and is
starting to search for you. They might head straight for a gate. They might try to pick up your trail on
the way. You really can’t guess. But if they reach the gate before you, you’re caught. If they happen
across your trail, the hound will pick up your scent. The dog knows your scent already — this isn’t
your first offense! The constable will loose the hound, who can run fast once he has the trail to follow.
You have a dilemma. If you are absolutely sure that you can reach the gates before the guard and
before being overtaken by the hound, you can keep the necklace. But if you aren’t sure, you need to
drop the necklace right now into the nearest pile of rubbish and saunter casually away. Even if they
grab you, without the necklace in your hands they will eventually release you.
So, keep the necklace or drop the necklace?
The town is modeled as a rectangular maze of discrete squares. It is surrounded by a wall that
contains one or more exits. You know, of course, your own position within the town. You also know
the location of the kennel where the constable and the hound start out.
1. In each turn (unit of time), you, the constable, and the hound move simultaneously.
2. You can move zero or one square(s) horizontally or vertically per turn.
3. Initially, the constable and hound move together, also zero or one square(s) vertically or horizon-
tally per turn.
4. If the constable and the hound, moving together, reach a square that you have previously occupied,
the hound catches your scent, and the constable looses the hound. On each subsequent turn, the
hound follows your trail at a speed of 1 or 2 square(s) per turn.
5. If the constable and/or the hound overtake you (occupy the same square as you), you are caught.
To escape, you must reach an exit at least one turn before the constable and/or hound.
Input
Input consists of one or more mazes. Each maze begins with a line containing two integers, W and H,
denoting the width and the height of the maze. End of input is indicated when either of these values
is less than 3. Neither dimension is greater than 50.
This is followed by H lines of input, each containing W characters.
The interpretation of the characters in these lines is as follows:
1. The space character ( ) denotes an open space.
2. ‘K’ is an open space denoting the kennel which is the starting position of the constable and the
hound. There will be exactly one of these in any maze.
3. ‘T’ is an open space denoting the original position of the thief (you). There will be exactly one of
these in any maze.
4. ‘X’ denotes a wall.ACM-ICPC Live Archive: 7297 – Hounded by Indecision
2/2
5. ‘E’ is an open space representing an exit (city gate). All exits will occur on the outer perimeter
(as defined by the W and H values) of the maze.
All mazes will be completely enclosed by a combination of ‘X’ and ‘E’ characters. There is at least
one path between the kennel and you, and there is at least one path between you and each exit.
Output
For each maze, print a single line of output. If there is a path that you can take that will guarantee
that you can escape, no matter what path what the constable and hound take, then print ‘KEEP IT’. If
there is no path that offers such a guarantee, print ‘DROP IT’.
Notes about the sample below:
In the first case, the constable and his hound can discover, on turn 7, the space where the thief had
stood on turn 4. The hound is loosed and overtakes the thief on turn 10 (or earlier if the thief doubles
back).
In the second case, the thief can reach the city gate in 13 turns, at which point the constable and
hound hit his trail. However, the thief is still able to get out of the city before the hound catches him.
Sample Input

19 11
XXXXXXXXXXXXXXXXXXX
X                                        X
E                                       X
X    XXX XXX K                X
X X X X
X X X X
X X T X X
X X X X
X XXXXXXX X
X X
XXXXXXXXXXXXXXEXXXX
19 11
XXXXXXXXXXXXXXXXXXX
X X
E X
X XXX XXX K X
X X X X
X X X X
X X T X X
X X X
X XXXXXXX X
X X
XXXXXXXXXXXXXXEXXXX

0 0
Sample Output
DROP IT
KEEP IT

题意:在n*m的地图中,有一个小偷偷了一个项链,一名警察和一条警犬来抓他,小偷和警察具有相同的速度,当警犬扑捉到小偷的气味时,警犬会以两倍的速度去追,若小偷在警犬和警察之前到达出口,则输出KEEP IT,反之他必须丢下项链保命,此时输出DROP IT

X:代表墙

K:代表警察和警犬的初始位置

T:代表小偷的初始位置

E:代表出口

‘ ’:代表可以走的路

题解:如果我们要采取最优的策略,先用BFS搜索一遍警察到任意一个网格的最小距离,当BFS搜索小偷,当小偷到达一个网格时,需要考虑如下几件事:

题目就是让你搜索出口,并且看是否有方案满足不被警察追到。
其实我们只要解决如何判断某一个点能不能走,如果这个解决了,那么就依次判断下去就可以了,最后看是否可以到达出口。(记为A点)
一:收先可以判断的是小偷必须在警察之前到达A点(要保证一定不被抓),计小偷到A的时间为T1,警察到A点的时间为T2,则T2>T1;
二:满足条件一后,当小偷到达A点以后,由于当警察到达A点之后,警犬会以两倍的速度追赶,所以之后小偷又一个最大的移动时间。即超过该时间会被警犬追上。
计算该时间:当警察到达A点时(距离小偷离开A点已经有T2-T1的时间了),小偷领先警察T2-T1,之后警犬以小偷2倍的速度追赶,可以知道警犬会花费T2-T1的时间追上小偷。所以小偷到达A点以后最大的移动时间为Limit_Time=2*(T2-T1);
三:假设小偷在前一个点剩余的最大移动时间为T,那么在A点小偷的最大剩余移动时间为T-1,那么要保证不被警察抓到,在A点的剩余时间就应该取条件二与T-1的较小值(要保证在最坏的情况下小偷都能脱身),即Limit_Time=min(T-1,Limit_Time),然后每搜到一个点记录在该点的最大剩余时间。当重复搜到某个点时,如果后者可以使最大剩余时间变大(说明有更优的方案到达该点),则入队,并更新当前点最多剩余时间,否则不入队

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=1e3+;
const int INF=0x3f3f3f3f;
int m,n;
char str[N][N];//地图
int vis[N][N];//标记数组
int arr[N][N];//记录警察到任意一点的最短路
int res[N][N];//小偷在某一点的最大剩余时间
int dis[][]= {{,-},{,},{-,},{,}};
struct node
{
int from;
int to;
int step;
int res_step;
};
void police_bfs(int a,int b);
int thief_bfs(int a,int b);
void solve()//输入
{
while(scanf("%d%d",&n,&m)!=-&&(m||n))
{
int x,y,dx,dy;
int t1=,t2=-;
char ch;
getchar();
for(int i=; i<m; i++)
{
for(int j=; j<n; j++)
{
ch=getchar();
str[i][j]=ch;
if(str[i][j]=='K')
dx=i,dy=j;
if(str[i][j]=='T')
x=i,y=j;
}
getchar();
}
police_bfs(dx,dy);
t2=thief_bfs(x,y);
if(t2==true)
cout<<"KEEP IT"<<endl;
else
cout<<"DROP IT"<<endl;
}
}
void police_bfs(int x,int y)//求出警察到达途中任意一点的时间
{
node p,s;
queue<node>q;
memset(arr,-,sizeof(arr));
memset(vis,,sizeof(vis));
p.from=x;
p.to=y;
p.step=;
vis[x][y]=;
arr[x][y]=;
q.push(p);
while(!q.empty())
{
s=q.front();
q.pop();
for(int i=; i<; i++)
{
p.from=s.from+dis[i][];
p.to=s.to+dis[i][];
p.step=s.step+;
if(p.from>=&&p.from<m&&p.to>=&&p.to>=&&p.to<n&&str[p.from][p.to]!='X'&&!vis[p.from][p.to])
{
arr[p.from][p.to]=p.step;
vis[p.from][p.to]=true;
q.push(p);
} }
}
}
int thief_bfs(int x,int y)
{
node p,s;
queue<node>q;
memset(res,,sizeof(res));
memset(vis,,sizeof(vis));
p.from=x;
p.to=y;
p.step=;
p.res_step=arr[x][y]*;
vis[x][y]=true;
res[x][y]=p.res_step;
q.push(p);
while(!q.empty())
{
s=q.front();
q.pop();
if(str[s.from][s.to]=='E')
{
return true;
}
for(int i=; i<; i++)
{
p.from=s.from+dis[i][];
p.to=s.to+dis[i][];
p.step=s.step+;
if(p.from>=&&p.from<m&&p.to>=&&p.to<n&&str[p.from][p.to]!='X')
{
if(p.step<arr[p.from][p.to])//如果到达该点的时间小于警察到达该点的时间,小偷才可以走这个点
{
p.res_step=min(s.res_step-,*(arr[p.from][p.to]-p.step));//保证在最坏的情况下小偷能逃生,s.res_step代表上一个点的最大剩余时间,所以取其最小值
}
else
continue;
if(p.res_step>res[p.from][p.to])//如果在该点的最大剩余时间大于先前更新的最大值如队列,保证求得的解是最小值
{ res[p.from][p.to]=p.res_step;
q.push(p);
} }
} }
return false;
}
int main()
{
solve();
return ;
}

UVALive-7297-Hounded by Indecision的更多相关文章

  1. UVALive 7297 Hounded by Indecision BFS

    题目链接:Hounded by Indecision 题意:map中给出小偷的位置,警察的位置.警察有一只狗,开始的时候警察和狗一起行动,也就是看做一个格子,当警察遇见小偷走过的格子时,狗就会嗅到它的 ...

  2. UVALive 7297 bfs

    题意 一个小偷偷到了项链 他想知道自己是否可以逃出去 地图中有一个小偷 一个警察 警察有一条狗 一开始 小偷和警察的移动速度都是1 当警察走到小偷经过过的地方时 警察会有一条狗嗅到小偷的气味并且以2的 ...

  3. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  4. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  5. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  6. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  7. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  8. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  9. UVALive 6500 Boxes

    Boxes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pract ...

随机推荐

  1. nginx会话保持之sticky模块

    nginx会话保持之nginx-sticky-module模块 在使用负载均衡的时候会遇到会话保持的问题,常用的方法有:1.ip hash,根据客户端的IP,将请求分配到不同的服务器上:2.cooki ...

  2. 让FireFox支持window.event属性

    场景描述: 在用户行为采集的过程中,需要侦听window下的event对象,根据事件类型做相应的过滤处理,但在firefox下window.event是未定义的: 问题分析: 要想获取event属性共 ...

  3. Qt enum使用总结

    一.enum 自省 const QMetaObject &mo = [ClassName]::staticMetaObject; int index = mo.indexOfEnumerato ...

  4. 1、lambda表达式

    lambda表达式中的类型是通过上下文推断出来的,类似String[] strArr = {"as","sd"};右边元素的子类型. 匿名内部类的情况:需要引用 ...

  5. BigDecimal 、BigInteger

    package com.BigDecimal; public class BigDecimalDemo { /* * 下面的运算的结果出乎我们的意料,有些准确,有些不准确 * 这是为什么呢? * 我们 ...

  6. 理解HTTP之Content-Type

    http://homeway.me/2015/07/19/understand-http-about-content-type/

  7. 每天一个linux命令(网络):【转载】ifconfig命令

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  8. Redis学习笔记-安装篇(Centos7)

    1.安装 这里使用源代码安装的方式,如果你希望使用yum或者rpm包安装的方式,可以百度一下,安装方法可谓多如牛毛. # 下载安装包 # wget http://download.redis.io/r ...

  9. Linq模型ObjectContext下查看Sql语句。

    ObjectContext 并没有提供 LINQ to SQL DataContext.Log 这样的功能,要查看实际生成的 T-SQL 语句,要么借助 SQL Server Sql Profiler ...

  10. 微软开源rDSN分布式系统开发框架

    摘要:微软亚洲研究院系统组开发的分布式系统开发框架——Robust Distributed System Nucleus(rDSN)正式在GitHub平台开源.据悉,rDSN是一个旨在为广大分布式系统 ...