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输出最小值。

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; char a[][];
int b[][],c[][];
int t[][]={{,},{,},{-,},{,-}}; struct Node{
int x,y,s;
}node; int main()
{
int n,m,yx,yy,mx,my,i,j;
while(~scanf("%d%d",&n,&m)){
memset(c,,sizeof(c));
queue<Node> q;
for(i=;i<n;i++){
getchar();
scanf("%s",a[i]);
for(j=;j<m;j++){
if(a[i][j]=='Y'){
yx=i;
yy=j;
}
if(a[i][j]=='M'){
mx=i;
my=j;
}
}
}
memset(b,,sizeof(b));
b[yx][yy]=;
node.x=yx;
node.y=yy;
node.s=;
q.push(node);
while(q.size()){
for(i=;i<;i++){
int tx=q.front().x+t[i][];
int ty=q.front().y+t[i][];
if(tx<||ty<||tx>=n||ty>=m) continue;
if(a[tx][ty]=='#'||b[tx][ty]==) continue;
b[tx][ty]=;
if(a[tx][ty]=='@'){
c[tx][ty]=q.front().s+;
}
node.x=tx;
node.y=ty;
node.s=q.front().s+;
q.push(node);
}
q.pop();
}
memset(b,,sizeof(b));
b[mx][my]=;
node.x=mx;
node.y=my;
node.s=;
q.push(node);
while(q.size()){
for(i=;i<;i++){
int tx=q.front().x+t[i][];
int ty=q.front().y+t[i][];
if(tx<||ty<||tx>=n||ty>=m) continue;
if(a[tx][ty]=='#'||b[tx][ty]==) continue;
b[tx][ty]=;
if(a[tx][ty]=='@'){
c[tx][ty]+=q.front().s+;
}
node.x=tx;
node.y=ty;
node.s=q.front().s+;
q.push(node);
}
q.pop();
}
int min=;
for(i=;i<n;i++){
for(j=;j<m;j++){
if(c[i][j]<min&&c[i][j]!=) min=c[i][j];
}
}
printf("%d\n",min);
}
return ;
}

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. JavaScript中批量设置Css样式

    设置 input 元素的  属性: document.getElementsByTagName("INPUT")[0].setAttribute("属性",&q ...

  2. Hibernate基础知识介绍

    一.什么是Hibernate? Hibernate,翻译过来是冬眠的意思,其实对于对象来说就是持久化.持久化(Persistence),即把数据(如内存中的对象)保存到可永久保存的存储设备中(如磁盘) ...

  3. Netty 100万级高并发服务器配置

    前言 每一种该语言在某些极限情况下的表现一般都不太一样,那么我常用的Java语言,在达到100万个并发连接情况下,会怎么样呢,有些好奇,更有些期盼. 这次使用经常使用的顺手的netty NIO框架(n ...

  4. Linux就该这么学--命令集合1(常用系统工作命令)

    1.用echo命令查看SHELL变量的值(前面有$符号): echo $SHELL 2.查看本机主机名: echo $HOSTNAME 3.查看当前的系统时间: date 4.按照“年-月-日 时:分 ...

  5. 记一次FastJSON和Jackson解析json时遇到的中括号问题

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jadyer/article/details/24395015 完整版见https://jadyer. ...

  6. vim实现代码缩进和可视区域的字符串替换

    今天2014年9月12号,实现了vim下的代码自动缩进和替换可视区域的字符串,之前一直在用vim这个强大的编辑器,它的强大只有用了的人才知道,现在把这两个很强大的功能展示出来,有个这两个功能,即使你写 ...

  7. 03-树2 List Leaves(25 point(s)) 【Tree】

    03-树2 List Leaves(25 point(s)) Given a tree, you are supposed to list all the leaves in the order of ...

  8. Discuz/X3.1去掉标题中的Powered by Discuz!以及解决首页标题后的"-"

    虽然不提倡大家去掉版权信息,但是在实际操作的时候还是去掉,毕竟每个页面标题最后面出现”Powered by Discuz!“会显得页面标题比较冗长. 经过本人的实践,论坛里也有操作方法,不过那个操作方 ...

  9. __FILE__,__LINE__,__func__ 真好用,DEBUG利器啊!

    我是不喜欢用类似VC下的F5,F10.曾经很喜欢用.被代码逻辑逼的没招了.所以不喜欢用了. 比如,错误是根据动态数据,产生的行为错误,无论是该写的未写,还是不该写的写了.指针跑飞什么等等,无非就是上述 ...

  10. tomcat正常启动,但是java项目没有启动原因

    右键项目,选择properties,查看该属性配置的是否正确