HDU - 2612 Find a way 双起点bfs(路径可重叠:两个队列分别跑)
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
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.
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
题意: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(路径可重叠:两个队列分别跑)的更多相关文章
- 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 ...
- 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 ...
- hdu 2612:Find a way(经典BFS广搜题)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2612 -Find a way (注重细节BFS)
主题链接:Find a Way 题目不难,前几天做,当时准备写双向BFS的,后来处理细节上出了点问题,赶上点事搁置了.今天晚上重写的,没用双向,用了两次BFS搜索,和双向BFS 道理差点儿相同.仅仅是 ...
- FZU2150 :Fire Game (双起点BFS)
传送门:点我 题意:“#”是草,"."是墙,询问能不能点燃俩地方,即点燃俩“#”,把所有的草烧完,如果可以,那么输出最小需要的时间,如果不行输出-1 思路:暴力BFS,看到n和m都 ...
- 题解报告:hdu 2612 Find a way(双bfs)
Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...
- HDU 2612 - Find a way - [BFS]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- BFS(最短路) HDU 2612 Find a way
题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...
随机推荐
- IOS AFNETWORKING POST
IOS AFNETWORKING POST 请求 #pragma mark post 请求 // 获取 url 路劲,不带参数 NSString *requestUrl = [[url compone ...
- iOS UI13_数据解析XML_,JSON
- (IBAction)parserButton:(id)sender { parserXML *parser =[[parserXML alloc] init]; [parser startPars ...
- 【BZOJ4293】[PA2015]Siano 线段树
[BZOJ4293][PA2015]Siano Description 农夫Byteasar买了一片n亩的土地,他要在这上面种草. 他在每一亩土地上都种植了一种独一无二的草,其中,第i亩土地的草每天会 ...
- Dropout: A Simple Way to Prevent Neural Networks fromOverfitting
https://www.cs.toronto.edu/~hinton/absps/JMLRdropout.pdf Deep neural nets with a large number of par ...
- struts2的核心和工作原理 (转)
转自--------http://blog.csdn.net/laner0515/article/details/27692673 在学习struts2之前,首先我们要明白使用struts2的目的是什 ...
- 前端photoshop 切图神器cutterman
1. 切图真的是就件很费力的事情,接下有给大家提供一个工具,本人觉得还不错 http://www.cutterman.cn/ 请参考这个网站,安装方法也有,很简单,我就不说了,赶紧点连接去注册帐号吧
- OC浅析一
Objective-C是一门简单的语言,95%是C.只是在语言层面上加了些关键字和语法.真正让Objective-C如此强大的是它的运行时.它很小但却很强大.它的核心是消息分发. 在Objective ...
- MongoDB 倾向于将数据都放在一个 Collection 下吗?
不是这样的. Collection 的单个 doc 有大小上限,现在是 16MB,这就使得你不可能把所有东西都揉到一个 collection 里.而且如果 collection 结构过于复杂,既会影响 ...
- Bestcoder round 18----B题(一元三次方程确定区间的最大值(包含极值比较))
Math Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- tomcat正常启动,但是java项目没有启动原因
右键项目,选择properties,查看该属性配置的是否正确