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. 一. Spring框架防XXS跨站攻击

    使用 Spring 框架进行 Java Web 开发,可以在 web.xml 文件中设置 HTML encode,在 JSP 文件页面元素 form 中确定实施. web.xml 加上: <co ...

  2. mysql中limit的用法详解[数据分页常用]

    在我们使用查询语句的时候,经常要返回前几条或者中间某几行的数据,这个时候怎么办呢?不用担心,mysql已经为我们提供了这样一个功能. SELECT * FROM table LIMIT [offset ...

  3. js 判断一个对象是否为空

    由于对于一个空对象{},其boolean值也是真,所以不能简单的用boolean来判断: jQuery的源码里有一个判断空对象的方法 function isEmptyObject(a) { var b ...

  4. Struts2基本使用(二)--配置文件简述

    配置文件简述 引入Struts2框架之后项目中多了一个struts.xml,以及web.xml也多了一些代码 web.xml只要的功能就是拦截用户的请求其多出的代码如下: <filter> ...

  5. L198

    One of the most common birth defects throughout the world is a cleft lip. Babies born with a cleft l ...

  6. 2018c语言第3次作业

    6-1 输出月份英文名 1.设计思路 (1)主要描述题目算法 第一步:先定义一个指针数组. 第二步:根据for循环判断月份并返还月份字数. 2.实验代码 int getindex( char *s ) ...

  7. 简单实现MemCachedUtil

    package com.chauvet.utils.memcached; import com.chauvet.utils.ConfigUtil; import com.danga.MemCached ...

  8. @vue/cli 3配置文件vue.config.js

    const webpack = require('webpack') const path = require('path') // const CompressionWebpackPlugin = ...

  9. java中读取配置文件

    若是Javaweb项目,项目运行于tomcat或其他容器时,可以使用下面方式来获取文件的输入流 1.当属性文件放在src下面时 InputStream is = Thread.currentThrea ...

  10. QLoo graphql engine 学习二 基本试用(kubernetes)

    已经测试过docker&& docker-compose 的运行模式,下面测试下kubernetes的运行模式 kubernetes 我使用docker for mac qloo 安装 ...