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 Boot通过@ConditionalOnProperty来控制Configuration是否生效

    Spring boot中有个注解@ConditionalOnProperty,这个注解能够控制某个configuration是否生效. 具体操作是通过其两个属性name以及havingValue来实现 ...

  2. APP的六种loading加载样式,全在这...

    今天这篇文章是给大家分享的loading加载的设计,文章里面会有一些实例在这分享给大家! 大多数App都要与服务器进行数据的交换,App向服务器发出数据请求,服务器接收到请求之后向App传输相应数据, ...

  3. 黑暗世界的搜索引擎 https://fofa.so/ https://www.shodan.io https://www.zoomeye.org 查找设备漏洞

    from:http://www.freebuf.com/sectool/121339.html 什么是 Shodan? 首先,Shodan 是一个搜索引擎,但它与 Google 这种搜索网址的搜索引擎 ...

  4. TStringGrid的Rows索引值 和 Cells的 索引值, Row的赋值

    Caption := sgShopList.Rows[sgShopList.RowCount +].CommaText; Caption := sgShopList.Rows[sgShopList.R ...

  5. Kafka术语解释

    前一篇文章介绍了如何使用kafka收发消息,但是对于kafka的核心概念并没有详细介绍,这里将会对包括kafka基本架构以及消费者.生产者API涉及的术语进行说明.了解这些术语有助于更深入理解kafk ...

  6. CF1061F:Lost Root(交互&概率)

    The graph is called tree if it is connected and has no cycles. Suppose the tree is rooted at some ve ...

  7. python3 openpyxl基本操作

    #coding:utf-8 import xlrd import xlwt # 读写2007 excel import openpyxl import sys #读取设备sn # def readSN ...

  8. cobbler装机错误--Failed to create kernel channel,-22

    最近使用cobbler安装GPU的机器遇到了一个错误:Failed to create kernel channel,-22 经过各种搜索发现是英伟达的显卡与centos7的默认显卡驱动冲突导致. 网 ...

  9. mysql update 没有where 不能更新的安全保护设置

    http://www.cnblogs.com/wjoyxt/p/5620827.html    没有where 不能更新的安全保护设置 http://dev.yesky.com/429/3543292 ...

  10. ffmpeg C++程序编译时报__cxa_end_catch错误

    解决方法在编译sh中加上 -lsupc++ 即可. 2.STL模块函数找不到,链接失败stdc++/include/bits/stl_list.h:466: error: undefined refe ...