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的最短路,然后相加求最小值 */ /***************************************** ...
随机推荐
- Ubuntu16.04下Django项目的部署
起飞前的准备 # 首先在Ubuntu的当前用户zhang下新建data文件夹,然后在data文件夹下新建你的项目目录root@zhang-virtual-machine:/home/zhang/dat ...
- jquery+easyui主界面布局一例
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="workbench.aspx ...
- 并发回射服务器的最基本实现思路( fork )
前言 一个服务器,通常会在一段时间内接收到多个请求.如果非要等到处理完一个请求再去处理下一个,势必会造成大部分用户的不满( 尤其当有某个请求需要占用大量时间时 ).如何解决这个问题?让处理这些用户请求 ...
- EasyRTSPClient:基于live555封装的支持重连的RTSP客户端RTSPClient
今天先简单介绍一下EasyRTSPClient,后面的文章我们再仔细介绍EasyRTSPClient内部的设计过程: EasyRTSPClient:https://github.com/EasyDar ...
- 有返回值的Bookmark
首先代码创建Activity: public sealed class WaitForResponse<TResult>:NativeActivity<TResult> { p ...
- php date之间的相互转换
字符串转成date $str =date("Y-m-d H:i:s",strtotime("2011-12-12 14:23:01")); echo $str; ...
- Kafka理论学习
Kafka Consumer设计解析 http://www.jasongj.com/2015/08/09/KafkaColumn4/
- 使用adb命令查看android中的数据库
在采用数据库操作时,经常会出现查询或删除等操作语句执行失败,但是有找不到具体原因.下面将介绍一种命令行方式进行数据库操作,来验证android中的数据库操作语句是否正确等. 具体操作步骤如下: (1) ...
- 给第三方apk进行系统签名的几种方式【转】
本文转载自:http://blog.csdn.net/luzhenrong45/article/details/47733053 版权声明:本文为博主原创文章,未经博主允许不得转载. -------- ...
- Ubuntu Linux系统环境变量配置文件【转】
本文转载自:https://my.oschina.net/qinlinwang/blog/30471 Ubuntu Linux系统环境变量配置文件: /etc/profile : 在登录时,操作系统 ...