-->Nightmare Ⅱ

原题太复杂,直接简单的讲中文吧

Descriptions:

X表示墙

.表示路

M,G表示两个人

Z表示鬼

M要去找G但是有两个鬼(Z)会阻碍他们,每一轮都是M和G先走M能走3步,G能走1步,Z每次向边上2步内变出分身。求所需最短时间。

鬼能穿墙,人不能

Sample Input

  1. 3
  2. 5 6
  3. XXXXXX
  4. XZ..ZX
  5. XXXXXX
  6. M.G...
  7. ......
  8. 5 6
  9. XXXXXX
  10. XZZ..X
  11. XXXXXX
  12. M.....
  13. ..G...
  14.  
  15. 10 10
  16. ..........
  17. ..X.......
  18. ..M.X...X.
  19. X.........
  20. .X..X.X.X.
  21. .........X
  22. ..XX....X.
  23. X....G...X
  24. ...ZX.X...
  25. ...Z..X..X

Sample Output

  1. 1
  2. 1
  3. -1

题目链接:
https://vjudge.net/problem/HDU-3085

设step为人一共走的轮数,每轮M走3步,G走1步,Z走2步

假设第step轮M、G碰面,碰面的地方为tmp,两个鬼为 zz[i],i可以是0或1表示两个鬼的编号,设横纵坐标分别为x,y

则tmp和zz[i]的距离为

abs(tmp.x-zz[i].x)+abs(tmp.y-zz[i].y)  这个即是曼哈顿距离

鬼在step轮可以走2*step步

若  abs(tmp.x-zz[i].x)+abs(tmp.y-zz[i].y)<=2*step

那就表示这个地方鬼能来,则这个地方不能碰面

相反若  abs(tmp.x-zz[i].x)+abs(tmp.y-zz[i].y)>2*step

那就表示这个地方鬼不能来,则这个地方能碰面

具体操作见代码

AC代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <fstream>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <deque>
  7. #include <vector>
  8. #include <queue>
  9. #include <string>
  10. #include <cstring>
  11. #include <map>
  12. #include <stack>
  13. #include <set>
  14. #include <sstream>
  15. #define mod 1000000007
  16. #define eps 1e-6
  17. #define ll long long
  18. #define INF 0x3f3f3f3f
  19. #define MEM(x,y) memset(x,y,sizeof(x))
  20. #define Maxn 1000
  21. using namespace std;
  22. int T,n,m;
  23. int step;//走了几轮
  24. char mp[Maxn][Maxn];//原始地图
  25. int dt[][]= {{,},{-,},{,},{,-}};//四个方向
  26. struct node
  27. {
  28. int x,y;//坐标
  29. };
  30. node now,net;
  31. node zz[],mm,gg;//鬼 M G
  32. queue<node>q[];//分别表示队列M和G
  33. queue<node>qt;//后面函数要用到,过渡
  34. bool judge(node tmp)
  35. {
  36. for(int i=; i<; i++)
  37. {
  38. if(abs(tmp.x-zz[i].x)+abs(tmp.y-zz[i].y)<=step*||mp[tmp.x][tmp.y]=='X'||mp[tmp.x][tmp.y]=='\0')
  39. return false;
  40. }
  41. return true;
  42. }
  43. bool bfs(int pos,int steps,char start,char endd)//队列的编号 M或G可以走的步数 开始标志 结束标志
  44. {
  45. qt=q[pos];//替代,后面要根据不同的steps进行走路
  46. for(int j=; j<steps; j++)//这一轮走几步
  47. {
  48. while(!qt.empty())
  49. {
  50. now=qt.front();
  51. qt.pop();
  52. q[pos].pop();
  53. if(!judge(now))//不满足
  54. continue;
  55. for(int i=; i<; i++)//四个方向
  56. {
  57. net.x=now.x+dt[i][];
  58. net.y=now.y+dt[i][];
  59. if(!judge(net)||mp[net.x][net.y]==start)
  60. continue;
  61. if(mp[net.x][net.y]==endd)//M找到G或G找到M
  62. return true;
  63. mp[net.x][net.y]=start;//将走过的地方表示为开始标志,即M或G已经来过这
  64. q[pos].push(net);
  65. }
  66. }
  67. qt=q[pos];//都向外走了一步,重新初始化qt,再向外走一步
  68. }
  69. return false;
  70. }
  71. int solve()
  72. {
  73. step=;//初始化
  74. while(!q[].empty())//初始化队列,清空
  75. q[].pop();
  76. while(!q[].empty())
  77. q[].pop();
  78. while(!qt.empty())
  79. qt.pop();
  80. q[].push(mm);//分别入队
  81. q[].push(gg);
  82. while(!q[].empty()&&!q[].empty())
  83. {
  84. step++;//走了几轮
  85. if(bfs(,,'M','G')||bfs(,,'G','M'))
  86. return step;
  87. }
  88. return -;
  89. }
  90. int main()
  91. {
  92. scanf("%d",&T);//这里都要用scanf和printf,不然会超时
  93. while(T--)
  94. {
  95. scanf("%d%d",&n,&m);
  96. MEM(mp,'X');//把地图初始化为X
  97. for(int cnt=,i=; i<=n; i++)
  98. {
  99.  
  100. scanf("%s",&mp[i][]);//一行一行存地图
  101. for(int j=; j<=m; j++)
  102. {
  103. // scanf("%c",&mp[i][j]);
  104. // cin>>mp[i][j];
  105. if(mp[i][j]=='M')
  106. {
  107. mm.x=i;
  108. mm.y=j;
  109. }
  110. if(mp[i][j]=='G')
  111. {
  112. gg.x=i;
  113. gg.y=j;
  114. }
  115. if(mp[i][j]=='Z')
  116. {
  117. zz[cnt].x=i;
  118. zz[cnt].y=j;
  119. cnt++;
  120. }
  121. }
  122. }
  123. printf("%d\n",solve());
  124. }
  125. }

【HDU - 3085】Nightmare Ⅱ(bfs)的更多相关文章

  1. 【HDU - 3533】Escape(bfs)

    Escape  Descriptions: 一个人从(0,0)跑到(n,m),只有k点能量,一秒消耗一点,在图中有k个炮塔,给出炮塔的射击方向c,射击间隔t,子弹速度v,坐标x,y问这个人能不能安全到 ...

  2. 【HDU 3085】 Nightmare Ⅱ

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3085 [算法] 双向BFS [代码] #include<bits/stdc++.h> ...

  3. 【HDU 2853】Assignment (KM)

    Assignment Problem Description Last year a terrible earthquake attacked Sichuan province. About 300, ...

  4. 【POJ - 3414】Pots(bfs)

    Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i)        将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...

  5. 【Aizu - 0558】Cheese(bfs)

    -->Cheese 原文是日语,这里就写中文了 Descriptions: 在H * W的地图上有N个奶酪工厂,每个工厂分别生产硬度为1-N的奶酪.有一只老鼠准备从出发点吃遍每一个工厂的奶酪.老 ...

  6. 【HDU - 4345 】Permutation(DP)

    BUPT2017 wintertraining(15) #8F 题意 1到n的排列,经过几次置换(也是一个排列)回到原来的排列,就是循环了. 现在给n(<=1000),求循环周期的所有可能数. ...

  7. 【HDU 6005】Pandaland(Dijkstra)

    Problem Description Mr. Panda lives in Pandaland. There are many cities in Pandaland. Each city can ...

  8. 【HDU - 6581】Vacation(思维)

    Vacation 题意 有n+1辆车,属性有长度l,距离终点的距离s,速度v问你最末尾的车到达终点的时间 Sample Input 1 2 2 7 1 2 1 2 1 2 2 10 7 1 6 2 1 ...

  9. 【HDU 5750】Dertouzos(数学)

    题目给定n和d,都是10的9次方以内,求1到n里面有几个数最大因数是d?1000000组数据.解:求出d的满足p[i]*d<n的最小质因数是第几个质数.即为答案. #include<cst ...

随机推荐

  1. Raw-OS备用事件源代码分析

    作为分析的内核版本2014-04-15,基于1.05正式版,blogs我们会跟上的内核开发进度的最新版本,如果出现源代码的目光"???"的话.没有深究的部分是理解. Raw-OS官 ...

  2. JAVA 加密方法

    1. RSA非对称加密 生成密钥对代码: //生成秘钥对 public static KeyPair getKeyPair() throws NoSuchAlgorithmException { Ke ...

  3. NYOJ 298 相变点(矩阵高速功率)

    点的变换 时间限制:2000 ms  |  内存限制:65535 KB 难度:5 描写叙述 平面上有不超过10000个点.坐标都是已知的.如今可能对全部的点做下面几种操作: 平移一定距离(M),相对X ...

  4. 索引Index

    优缺点 索引是对数据库表中一列或多列的值进行排序的一种结构为了提高查询的效率索引一般建立在需要经常查询的地方 优点 创建索引可以大大提高系统的性能第一,通过创建唯一性索引,可以保证数据库表中每一行数据 ...

  5. Wrapped的返回值取值

    Bared   Wrapped   using Newtonsoft.Json; using Newtonsoft.Json.Linq; string str = JsonConvert.Serial ...

  6. Bootstrap 标签徽章巨幕页头

    @{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport&q ...

  7. Java Socket 爬虫

    # 地址 https://github.com/mofadeyunduo/crawler # 前言 1.代码不断优化更新. 2.有建议请留言. # 介绍 1.多线程,基于 ExcutorServcie ...

  8. WPF扩展标记X:STATIC

    原文:WPF扩展标记X:STATIC public class XStaic     {         public static string Content = "确定"; ...

  9. 在 Laravel 中通过 Artisan View 扩展包创建及删除应用视图文件

    1.简介 本扩展包添加了两个视图相关的Artisan命令到Laravel应用,以便我们通过Artisan命令即可创建和管理视图文件,可谓是进一步解放了生产力. 2.安装 还是通过Composer安装: ...

  10. C# Oracle数据库操作类

    using System; using System.Data; using System.Collections.Generic; using System.Configuration; using ...