题目链接:Hounded by Indecision

题意:map中给出小偷的位置,警察的位置。警察有一只狗,开始的时候警察和狗一起行动,也就是看做一个格子,当警察遇见小偷走过的格子时,狗就会嗅到它的气味,以2倍的速度追捕小偷。现在,小偷希望知道自己是否有安全路线逃出去,以此决定是否要继续拿着偷的东西。出口不止一处,只有一个警察和一只狗,一个小偷。【知道也才不告诉他,哼。】

思路:因为小偷每走到一个格子时,时间为t1,决定他是否被抓到的因素是警察到这里的时间t2。当t1>=t2时,小偷被抓,当t1<t2时,小偷还能从该点继续走,直到t2-t1+t2时刻被狗抓住或者跑出去。所以首先对警察bfs,确定警察到map上的每个格子最短时间t2。然后,对小偷bfs,判断当前点是否能走的条件:map范围内且不是围墙 && 到达时间t1<t2 && 还没有到限制的时间 && (没有走过 || 之前走到当前路线的时间比现在小)。注意:我们每到一个新的点,当前点的限制是前一个点的限制和当前点计算得到的限制的max值,而我们可以选择一条路线使得再次到达当前点的步数更小,获得的逃跑时间更长。

【事实证明:"之前走到当前路线的时间比现在小"条件可以去掉,因为本题中 任何位置bfs第一次搜索到的时间,一定是最短时间。】

代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <math.h>
  5. #define maxn 210
  6. #define inf 0x7f7f7f7f
  7. using namespace std;
  8.  
  9. int step[maxn][maxn];
  10. char mp[maxn][maxn];
  11. int vis[maxn][maxn]; // lim
  12. int dir[4][2] = {1,0, -1,0, 0,1, 0,-1};
  13. int w, h;
  14.  
  15. struct Node {
  16. int x, y, lim, step;
  17. Node () {
  18. lim = inf;
  19. step = inf;
  20. }
  21. }q[maxn*maxn*maxn], st;
  22.  
  23. struct Point{
  24. int x, y;
  25. }que[maxn*maxn*maxn], now, nxt, temp;
  26.  
  27. bool check(int x, int y) {
  28. if (x>=0 && x<h && y>=0 && y<w && mp[x][y] != 'X')
  29. return true;
  30. return false;
  31. }
  32.  
  33. void bfs_police(int px, int py){
  34. memset(vis, 0, sizeof(vis));
  35. vis[px][py] = 1;
  36. step[px][py] = 0;
  37. now.x = px, now.y = py;
  38. int head = 0, tail = -1;
  39. que[++tail] = now;
  40. while(head <= tail) {
  41. now = que[head++];
  42. for (int i=0; i<4; ++i) {
  43. nxt.x = now.x + dir[i][0];
  44. nxt.y = now.y + dir[i][1];
  45. if (check(nxt.x, nxt.y) && vis[nxt.x][nxt.y] == 0) {
  46. step[nxt.x][nxt.y] = step[now.x][now.y] + 1;
  47. vis[nxt.x][nxt.y] = 1;
  48. que[++tail] = nxt;
  49. }
  50. }
  51. }
  52. }
  53.  
  54. bool bfs_thief(Node st) {
  55. memset(vis, 0, sizeof(vis));
  56. st.step = 0;
  57. st.lim = (step[st.x][st.y] - st.step) + step[st.x][st.y];
  58. int head = 0, tail = -1;
  59. q[++tail] = st;
  60. vis[st.x][st.y] = st.lim;
  61.  
  62. while(head <= tail) {
  63. Node now = q[head++];
  64. if (mp[now.x][now.y] == 'E') {
  65. return true;
  66. }
  67. for (int i=0; i<4; ++i) {
  68. Node nxt = now;
  69. nxt.x += dir[i][0];
  70. nxt.y += dir[i][1];
  71. nxt.step++;
  72. if (check(nxt.x, nxt.y)) {
  73. int lim = step[nxt.x][nxt.y] + (step[nxt.x][nxt.y] - nxt.step);
  74. if (lim < nxt.lim) nxt.lim = lim;
  75. 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])) {
  76. vis[nxt.x][nxt.y] = nxt.lim;
  77. q[++tail] = nxt;
  78. }
  79. }
  80. }
  81. }
  82. return false;
  83. }
  84.  
  85. int main() {
  86. //freopen("in.cpp", "r", stdin);
  87. while(~scanf("%d%d", &w, &h)) {
  88. if (w<3 || h<3) break;
  89. getchar();
  90. int px, py;
  91. for (int i=0; i<h; ++i) {
  92. gets(mp[i]);
  93. int len = strlen(mp[i]);
  94. for (int j=0; j<len; ++j) {
  95. if (mp[i][j] == 'K') {
  96. px = i, py = j;
  97. }
  98. else if (mp[i][j] == 'T') {
  99. st.x = i, st.y = j;
  100. }
  101. }
  102. }
  103. bfs_police(px, py);
  104. bool esc = bfs_thief(st);
  105. if (esc) printf("KEEP IT\n");
  106. else printf("DROP IT\n");
  107. }
  108. return 0;
  109. }

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

  1. UVALive 7297 bfs

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

  2. UVALive 6665 Dragon’s Cruller --BFS,类八数码问题

    题意大概就是八数码问题,只不过把空格的移动方式改变了:空格能够向前或向后移动一格或三格(循环的). 分析:其实跟八数码问题差不多,用康托展开记录状态,bfs即可. 代码: #include <i ...

  3. UVALive 3956 Key Task (bfs+状态压缩)

    Key Task 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/D Description The Czech Technica ...

  4. UVALive 2520 Holedox Moving(BFS+状态压缩)

    这个题目在比赛的时候我们是没有做出来的,但是听到他们说进制哈希的时候,感觉真的是挺高端的,于是赛后开始补题,本着我的习惯在看题解之前自己再试着写一遍,我当时存储状态的方法是string + map,我 ...

  5. UVALive 2035 The Monocycle(BFS状态处理+优先队列)

    这道题目真是非常坎坷啊,WA了很多次,但所有的思路都是奔着广搜去想的,一开始出现了比答案大的数据,才想到了应该是优先队列,再说加上也肯定不会错.一开始我读错了题意,以为旋转并且前行需要的时间跟其他一样 ...

  6. UVALive 6485 Electric Car Rally (BFS,PQ)

    https://icpcarchive.ecs.baylor.edu/index.php? option=com_onlinejudge&Itemid=8&page=show_prob ...

  7. UVALive-7297-Hounded by Indecision

    OK, maybe stealing the Duchess’s favorite ruby necklace was not such a good idea. You were makingyou ...

  8. What a Ridiculous Election UVALive - 7672 (BFS)

    题目链接: E - What a Ridiculous Election  UVALive - 7672 题目大意: 12345 可以经过若干次操作转换为其它五位数. 操作分三种,分别为: 操作1:交 ...

  9. UVALive 5066 Fire Drill BFS+背包

    H - Fire Drill Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Sta ...

随机推荐

  1. js 定时器的使用。 setInterval()

    我需要实现的功能是:点击发送按钮,会出现 “已发送60s后可点击重发”,并且,60s 这个数字是随时变化的,60,59,58,57....0,然后再次返回到 发送 按钮. 类似效果,可参考  360首 ...

  2. NPN&PNP

    一.晶体管基础知识 晶体管分2种:NPN.PNP 晶体管通常封装为TO-92,下面是元件实物图 和 元件符合: NPN: 当电压和电流被加到基极上时,NPN晶体管: 其工作原理: 就像水龙头—给控制开 ...

  3. E2 2014.6.3 更新日志

    增加功能 增加支持中关村获取商品信息 增加个人业绩查询功能 增加赠送和获赠查询功能 增加商品历程分析报表,资金历程分析报表,科目明细分析报表, 销售分析报表 增加服务维修明细表查询报表 完善功能 固定 ...

  4. iOS - AFNetworking 网络请求

    前言 在 iOS 开发中,一般情况下,简单的向某个 Web 站点简单的页面提交请求并获取服务器的响应,用 Xcode 自带的 NSURLConnection 是能胜任的.但是,在绝大部分下我们所需要访 ...

  5. Redis脚本插件之————执行Lua脚本示例

    Redis在2.6推出了脚本功能,允许开发者使用Lua语言编写脚本传到Redis中执行.使用脚本的好处如下: 1.减少网络开销:本来5次网络请求的操作,可以用一个请求完成,原先5次请求的逻辑放在red ...

  6. 【Todo】Mybatis学习-偏理论

    之前写过好几篇Mybatis相关的文章: http://www.cnblogs.com/charlesblc/p/5906431.html  <SSM(SpringMVC+Spring+Myba ...

  7. D3.js 交互式操作

    与图表的交互,指在图形元素上设置一个或多个监听器,当事件发生时,做出相应的反应. 一.什么是交互 交互,指的是用户输入了某种指令,程序接受到指令之后必须做出某种响应.对可视化图表来说,交互能使图表更加 ...

  8. jackson annotations注解详解 (zhuan)

    http://blog.csdn.net/sdyy321/article/details/40298081 ************************************** 官方WIKI: ...

  9. latex输入希腊字母

    \alpha产生字符α;\beta产生字符β:\gamma产生字符γ:\delta产生字符δ;\epsilon产生字符ε; \zeta产生字符ζ:\eta产生字符η;\theta产生字符9; \iot ...

  10. phalcon: plugin 结合Manager事件管理、dispatcher调度控制器 监听sql日志记录或其他拦截出来

    可能用到的类 phalcon\mvc\use\plugin Phalcon\Mvc\Dispatcher as MvcDispatcher Phalcon\Events\Manager as Even ...