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的最短路,然后相加求最小值 */ /***************************************** ...
随机推荐
- poj 1703 Find them, Catch them(种类并查集和一种巧妙的方法)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36176 Accepted: ...
- 各浏览器对常用或者错误的 Content-Type 类型处理方式不一致
标准参考 content-type 用于定义用户的浏览器或相关设备如何显示将要加载的数据,或者如何处理将要加载的数据,此属性的值可以查看 MIME 类型. MIME (Multipurpose Int ...
- JS控制input 文本框只允许输入汉字
onblur="value=value.replace(/[^/u4E00-/u9FA5]/g,'')" onbeforepaste="clipboardData.set ...
- sap保存时小写字母自己主动转大写字母的解决方法
在实际应用中.ABAP保存数据到后台数据库表中时.会自己主动把前台输入的小写字母自己主动转换为大写字母来保存.有时候客户可能不须要转换,就须要用到以下的方法: 1.找到相应字段的Data ...
- 【BZOJ4293】[PA2015]Siano 线段树
[BZOJ4293][PA2015]Siano Description 农夫Byteasar买了一片n亩的土地,他要在这上面种草. 他在每一亩土地上都种植了一种独一无二的草,其中,第i亩土地的草每天会 ...
- 九度OJ 1095:2的幂次方 (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:913 解决:626 题目描述: Every positive number can be presented by the exponent ...
- 图片转base64存储
图片转base64存储 base64.b64encode(r.content) url='http://www.heze.cn/info/themes/heze/Public/tel/?tel=MDU ...
- Local Response Normalization 60 million parameters and 500,000 neurons
CNN是工具,在图像识别中是发现图像中待识别对象的特征的工具,是剔除对识别结果无用信息的工具. ImageNet Classification with Deep Convolutional Neur ...
- hdu 3415 单调队列
Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- C++设计模式实现--策略(Strategy)模式
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/L_Andy/article/details/30489331 一. 举例说明 曾经做了一个程序,程序 ...