Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16184    Accepted Submission(s): 5194

Problem Description
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest. 
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
 
Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200). 
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’    express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
 
Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
 
Sample Input
4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
 
Sample Output
66
88
66
 
Author
yifenfei
 
Source
 
Recommend
yifenfei

题意:Y和M在两个不同起点,他们要到KFC集合,路上有多家KFC,问到哪家KFC能使他们的步数和最少?

思路:两边bfs,分别存取Y和M到各家KFC的步数,相加求和,枚举各KFC输出最小值。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. using namespace std;
  5.  
  6. char a[][];
  7. int b[][],c[][];
  8. int t[][]={{,},{,},{-,},{,-}};
  9.  
  10. struct Node{
  11. int x,y,s;
  12. }node;
  13.  
  14. int main()
  15. {
  16. int n,m,yx,yy,mx,my,i,j;
  17. while(~scanf("%d%d",&n,&m)){
  18. memset(c,,sizeof(c));
  19. queue<Node> q;
  20. for(i=;i<n;i++){
  21. getchar();
  22. scanf("%s",a[i]);
  23. for(j=;j<m;j++){
  24. if(a[i][j]=='Y'){
  25. yx=i;
  26. yy=j;
  27. }
  28. if(a[i][j]=='M'){
  29. mx=i;
  30. my=j;
  31. }
  32. }
  33. }
  34. memset(b,,sizeof(b));
  35. b[yx][yy]=;
  36. node.x=yx;
  37. node.y=yy;
  38. node.s=;
  39. q.push(node);
  40. while(q.size()){
  41. for(i=;i<;i++){
  42. int tx=q.front().x+t[i][];
  43. int ty=q.front().y+t[i][];
  44. if(tx<||ty<||tx>=n||ty>=m) continue;
  45. if(a[tx][ty]=='#'||b[tx][ty]==) continue;
  46. b[tx][ty]=;
  47. if(a[tx][ty]=='@'){
  48. c[tx][ty]=q.front().s+;
  49. }
  50. node.x=tx;
  51. node.y=ty;
  52. node.s=q.front().s+;
  53. q.push(node);
  54. }
  55. q.pop();
  56. }
  57. memset(b,,sizeof(b));
  58. b[mx][my]=;
  59. node.x=mx;
  60. node.y=my;
  61. node.s=;
  62. q.push(node);
  63. while(q.size()){
  64. for(i=;i<;i++){
  65. int tx=q.front().x+t[i][];
  66. int ty=q.front().y+t[i][];
  67. if(tx<||ty<||tx>=n||ty>=m) continue;
  68. if(a[tx][ty]=='#'||b[tx][ty]==) continue;
  69. b[tx][ty]=;
  70. if(a[tx][ty]=='@'){
  71. c[tx][ty]+=q.front().s+;
  72. }
  73. node.x=tx;
  74. node.y=ty;
  75. node.s=q.front().s+;
  76. q.push(node);
  77. }
  78. q.pop();
  79. }
  80. int min=;
  81. for(i=;i<n;i++){
  82. for(j=;j<m;j++){
  83. if(c[i][j]<min&&c[i][j]!=) min=c[i][j];
  84. }
  85. }
  86. printf("%d\n",min);
  87. }
  88. return ;
  89. }

HDU - 2612 Find a way 双起点bfs(路径可重叠:两个队列分别跑)的更多相关文章

  1. FZU 2150 Fire Game (高姿势bfs--两个起点)(路径不重叠:一个队列同时跑)

    Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows ...

  2. HDU 2612 Find a way(双向bfs)

    题目代号:HDU 2612 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 M ...

  3. hdu 2612:Find a way(经典BFS广搜题)

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. HDU 2612 -Find a way (注重细节BFS)

    主题链接:Find a Way 题目不难,前几天做,当时准备写双向BFS的,后来处理细节上出了点问题,赶上点事搁置了.今天晚上重写的,没用双向,用了两次BFS搜索,和双向BFS 道理差点儿相同.仅仅是 ...

  5. FZU2150 :Fire Game (双起点BFS)

    传送门:点我 题意:“#”是草,"."是墙,询问能不能点燃俩地方,即点燃俩“#”,把所有的草烧完,如果可以,那么输出最小需要的时间,如果不行输出-1 思路:暴力BFS,看到n和m都 ...

  6. 题解报告:hdu 2612 Find a way(双bfs)

    Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...

  7. HDU 2612 - Find a way - [BFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...

  8. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

  9. BFS(最短路) HDU 2612 Find a way

    题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...

随机推荐

  1. Ubuntu16.04下Django项目的部署

    起飞前的准备 # 首先在Ubuntu的当前用户zhang下新建data文件夹,然后在data文件夹下新建你的项目目录root@zhang-virtual-machine:/home/zhang/dat ...

  2. jquery+easyui主界面布局一例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="workbench.aspx ...

  3. 并发回射服务器的最基本实现思路( fork )

    前言 一个服务器,通常会在一段时间内接收到多个请求.如果非要等到处理完一个请求再去处理下一个,势必会造成大部分用户的不满( 尤其当有某个请求需要占用大量时间时 ).如何解决这个问题?让处理这些用户请求 ...

  4. EasyRTSPClient:基于live555封装的支持重连的RTSP客户端RTSPClient

    今天先简单介绍一下EasyRTSPClient,后面的文章我们再仔细介绍EasyRTSPClient内部的设计过程: EasyRTSPClient:https://github.com/EasyDar ...

  5. 有返回值的Bookmark

    首先代码创建Activity: public sealed class WaitForResponse<TResult>:NativeActivity<TResult> { p ...

  6. php date之间的相互转换

    字符串转成date $str =date("Y-m-d H:i:s",strtotime("2011-12-12 14:23:01")); echo $str; ...

  7. Kafka理论学习

    Kafka Consumer设计解析 http://www.jasongj.com/2015/08/09/KafkaColumn4/

  8. 使用adb命令查看android中的数据库

    在采用数据库操作时,经常会出现查询或删除等操作语句执行失败,但是有找不到具体原因.下面将介绍一种命令行方式进行数据库操作,来验证android中的数据库操作语句是否正确等. 具体操作步骤如下: (1) ...

  9. 给第三方apk进行系统签名的几种方式【转】

    本文转载自:http://blog.csdn.net/luzhenrong45/article/details/47733053 版权声明:本文为博主原创文章,未经博主允许不得转载. -------- ...

  10. Ubuntu Linux系统环境变量配置文件【转】

    本文转载自:https://my.oschina.net/qinlinwang/blog/30471 Ubuntu Linux系统环境变量配置文件:  /etc/profile : 在登录时,操作系统 ...