题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612

思路:从两个起点出发,有多个终点,求从两个起点同时能到达的终点具有的最小时间,开两个数组分别保存两个起点到达每一个终点的用时,最后将两个

数组里的时间加起来求最小的一组,必须对应相加,因为终点必须同时到达。

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <fstream>
#include <queue>
using namespace std;
struct node{
int x,y,step;
}a[1010];
node p,q;
int n,m,sx1,sy1,sx2,sy2,ans1[1010],ans2[1010],ans[1010];
int mmin,cnt;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
char maze[205][205];
bool visit[205][205];
int judge(int x,int y){
for(int i=1;i<cnt;i++)
{
if(x==a[i].x&&y==a[i].y)return i;
}
return 0;
}
void bfs1(int x,int y){
memset(ans,0,sizeof(ans));
memset(ans1,-1,sizeof(ans1));
memset(visit,0,sizeof(visit));
queue<node> Q;
p.x=x;
p.y=y;
p.step=0;
Q.push(p);
visit[p.x][p.y]=1;
while(!Q.empty())
{
p=Q.front();
Q.pop();
int num=judge(p.x,p.y);
if(num){
ans[num]=p.step;
visit[p.x][p.y]=1;
}
for(int i=0;i<4;i++)
{
q.x=p.x+dir[i][0];
q.y=p.y+dir[i][1];
q.step=p.step+1;
if(q.x<0||q.x>=n||q.y<0||q.y>=m)continue;
if(visit[q.x][q.y])continue;
if(maze[q.x][q.y]=='#')continue;
Q.push(q);
visit[q.x][q.y]=1;
}
}
for(int i=1;i<cnt;i++){
if(ans[i])ans1[i]=ans[i];
} }
void bfs2(int x,int y){
memset(ans,0,sizeof(ans));
memset(ans2,-1,sizeof(ans2));
memset(visit,0,sizeof(visit));
queue<node> Q;
p.x=x;
p.y=y;
p.step=0;
Q.push(p);
visit[p.x][p.y]=1;
while(!Q.empty())
{
p=Q.front();
Q.pop();
int num=judge(p.x,p.y);
if(num){
ans[num]=p.step;
visit[p.x][p.y]=1;
}
for(int i=0;i<4;i++)
{
q.x=p.x+dir[i][0];
q.y=p.y+dir[i][1];
q.step=p.step+1;
if(q.x<0||q.x>=n||q.y<0||q.y>=m)continue;
if(visit[q.x][q.y])continue;
if(maze[q.x][q.y]=='#')continue;
Q.push(q);
visit[q.x][q.y]=1;
}
}
for(int i=1;i<cnt;i++){
if(ans[i])ans2[i]=ans[i];
}
}
int main()
{
//ifstream fin;
//fin.open("data1.txt"); while(cin>>n>>m)
{
cnt=1;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
cin>>maze[i][j];
if(maze[i][j]=='Y'){
sx1=i;
sy1=j;
}
if(maze[i][j]=='M'){
sx2=i;
sy2=j;
}
if(maze[i][j]=='@'){
a[cnt].x=i;
a[cnt++].y=j;
}
}
mmin=999999;
bfs1(sx1,sy1);
bfs2(sx2,sy2);
for(int i=1;i<cnt;i++)
{
if(ans1[i]!=-1&&ans2[i]!=-1){
int tsum=ans1[i]+ans2[i];
if(mmin>tsum)mmin=tsum;
}
}
cout<<mmin*11<<endl;
}
return 0; }

HDU/HDOJ 2612 Find a way 双向BFS的更多相关文章

  1. Eight (HDU - 1043|POJ - 1077)(A* | 双向bfs+康拓展开)

    The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...

  2. hdoj 2612 Find a way【bfs+队列】

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

  3. 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 ...

  4. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  5. 2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以 ...

  6. HDU 1043 Eight(双向BFS+康托展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...

  7. HDU.2612 Find a way (BFS)

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

  8. HDU 3085 Nightmare Ⅱ(双向BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...

  9. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

随机推荐

  1. ios开发问题:添加库和复制其它工程文件编译错误问题

    首先添加库问题: 选择页签Build Phases->Link Binary With Libraries点+添加库 然后command+shift+k清除历史debug的垃圾 复制其它工程文件 ...

  2. java中的object类

    在Java中,任何一个类都扩展来自Object类.当没有为某一个类定义父类时,Java会自动定义Object类为其父类. object类的一些常用方法: (1)public String toStri ...

  3. NYOJ10,skiing

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪, 由于滑雪的确非常刺激.但是为了获得速度,滑的区域必须向下倾斜,并且 ...

  4. 【linux驱动笔记】字符设备驱动相关数据结构与算法

    欢迎转载,转载时需保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http:// ...

  5. 基于visual Studio2013解决C语言竞赛题之1022最大数最小数

         题目 解决代码及点评 /************************************************************************/ ...

  6. 设计模式6:Composite

    Entry.java: package gendwang.cisco.com; public abstract class Entry { private int height = 0; privat ...

  7. Java批量生成Mac地址到文件

    public class Main { public static void main(String[] args) { // 生成文件名称 String filePath = "mac.t ...

  8. ClusterWare 服务介绍

    一.ClusterWare启动流程图 二.Clusterware启动的代理进程 ­­­­­­­­­­­­­­­­­层次        代理进程        进程                    ...

  9. 基于visual Studio2013解决面试题之0305广度优先搜索二叉树

     题目

  10. 用asio的定时器实现带超时的connect,备忘

    // test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <boost/asio.hpp> #inclu ...