题目代号:HDU 2612

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

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15919    Accepted Submission(s): 5110

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

题目大意:有两个人,位置分别由Y与M代替,@是餐厅的位置,每个人移动到周围的点需要11分钟,现在求两个人到达某个餐厅的时间花费最少。

题目思路:双向bfs遍历所有的位置,分别用两个数组作为标记,如果碰到@就保存当前花费的步数,然后两个数组对应位置相加,然后找到总步数最小的那个的值乘以11,输出就行了

AC代码:

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL; const int MAXM=;
char a[MAXM][MAXM];
int n,m;
int cx[]= {-,,,};
int cy[]= {,,-,}; struct node
{
int x,y;
int flag;
int step;
}; queue<node>Q;
int vis1[MAXM][MAXM];
int vis2[MAXM][MAXM]; void bfs()
{
while(!Q.empty())
{
int x=Q.front().x;
int y=Q.front().y;
int flag=Q.front().flag;
int cnt=Q.front().step;
Q.pop();
for(int i=; i<; i++)
{
int tx=x+cx[i];
int ty=y+cy[i];
if(flag==)
{
if(vis1[tx][ty]==-)
{
if(a[tx][ty]=='@')
{
vis1[tx][ty]=cnt+;
}
else
{
vis1[tx][ty]=;
}
Q.push(node{tx,ty,,cnt+});
}
}
else
{
if(vis2[tx][ty]==-)
{
if(a[tx][ty]=='@')
{
vis2[tx][ty]=cnt+;
}
else
{
vis2[tx][ty]=;
}
Q.push(node{tx,ty,,cnt+});
}
}
}
}
} int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d%d",&n,&m))
{
mem(vis1,);
mem(vis2,);
for(int i=; i<=n; i++)
{
scanf("%s",a[i]+);
for(int j=; j<=m; j++)
{
if(a[i][j]=='Y')
{
Q.push(node{i,j,,});
vis1[i][j]=vis2[i][j]=-;
}
else if(a[i][j]=='M')
{
Q.push(node{i,j,,});
vis1[i][j]=vis2[i][j]=-;
}
else if(a[i][j]=='.'||a[i][j]=='@')
{
vis1[i][j]=vis2[i][j]=-;
}
}
}
bfs();
/*
FOR(i,1,n)
{
FOR(j,1,m)
{
printf("%3d",vis1[i][j]);
}
puts("");
}
puts("");
FOR(i,1,n)
{
FOR(j,1,m)
{
printf("%3d",vis2[i][j]);
}
puts("");
}
*/
int ans=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
vis1[i][j]+=vis2[i][j];
if(vis1[i][j]>)ans=min(ans,vis1[i][j]);
}
}
cout<<ans*<<endl;
}
return ;
}

HDU 2612 Find a way(双向bfs)的更多相关文章

  1. HDU.2612 Find a way (BFS)

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

  2. HDU——1195Open the Lock(双向BFS)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. HDU/HDOJ 2612 Find a way 双向BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 思路:从两个起点出发,有多个终点,求从两个起点同时能到达的终点具有的最小时间,开两个数组分别保存 ...

  4. HDU - 2612 Find a way 【BFS】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 题意 有两个人 要去一个城市中的KFC 一个城市中有多个KFC 求两个人到哪一个KFC的总时间最 ...

  5. hdu 2612 Find a way(BFS)

    题目链接:hdu2612 思路:题意是求两个人到某一个KFC花费时间和最小,其实就是求最短距离和,用两个BFS,分别以两个人为起点,分别记录下两人到每个KFC的距离,然后求出最小的和 #include ...

  6. (简单) HDU 2612 Find a way,BFS。

    Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Nin ...

  7. HDU - 2612 Find a way(BFS搜索)

    题目: 链接 思路: 用BFS分别以‘Y’和‘M’的位置为起点进行两次搜索,并把这两次的搜索结果在一个二维数组中保存下来,在对地图遍历遇到‘@’更行最小值. PS: 如果用‘Y’和‘M’点分别去搜每个 ...

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

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

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

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

随机推荐

  1. sql回显注入-笔记

     拼接sql命令查询数据   注释 常用于sql注入            # 井号 单行注释 注意:URL编码 %23          -- 两个减号加空格 单行注释           /*   ...

  2. strtoul()引起的刷卡异常

    )//10个字节 10进制数 { tempcard=strtoul((,);//将 cardnum[]=tempcard>>&0x00ff;//最高位 cardnum[]=temp ...

  3. Java作业 题目:16版.真实员工数统计

    题目:16版.真实员工数统计 该资源支持按部自动给分,评分规则如下: sjkdfhslkfdhdsiog函数定义测试 sjkdfhslkfdhdsiog函数定义测试 sjkdfhslkfdhdsiog ...

  4. hibernate-HQL连接查询 转载sincoolvip

    和SQL查询一样,HQL也支持各种各样的连接查询,如内连接.外连接 连接类型 HQL语法内连接 inner join 或者join迫切内连接 inner join fetch 或join fetch左 ...

  5. 原生js:click和onclick本质的区别(转https://www.cnblogs.com/web1/p/6555662.html)

    原生javascript的click在w3c里边的阐述是DOM button对象,也是html DOM click() 方法,可模拟在按钮上的一次鼠标单击. button 对象代表 HTML 文档中的 ...

  6. [WPF]BringIntoView

    1.在scrollview 中的frameworkelement可以使用 FE.BringIntoView(); 滚动到此控件. 2.该 方法能一个重载 Bottom.BringIntoView(ne ...

  7. IDEA配置远程git仓库

    一,在项目创建本地仓储 2,选择当前文件夹为本地仓储 3, 将代码添加到本地仓库 4,提交到本地仓库 5,提交到本地仓库 配置远程账号和地址 输入你自己的远程仓库----码云或者github,创建的 ...

  8. setInterval 定时器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. iOS App沙盒目录结构

    转自:http://blog.csdn.net/wzzvictory/article/details/18269713 出于安全考虑,iOS系统的沙盒机制规定每个应用都只能访问当前沙盒目录下面的文件( ...

  10. python之kafka消费

    使用python3第三方工具,实现kafka消费 # -*- coding: utf-8 -*- import uuid import json from kafka import KafkaCons ...