Find a way

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

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
两个起点,多个终点,两个人必须到达同一个终点,开两个数组,分别存放第一个和第二个人到达终点的步数,然后在对应终点出让两人的步数相加,取最小的
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define MAX 210
#define INF 0x3f3f3f
using namespace std;
int bu1[MAX][MAX];//记录第一个人步数
int bu2[MAX][MAX];//记录第二个人步数
int p;
char map[MAX][MAX];
int vis[MAX][MAX];
int n,m;
struct node
{
int x,y;
int step;
};
int MIN(int x,int y)
{
return x<y?x:y;
}
void bfs(int x1,int y1,int p)
{
memset(vis,0,sizeof(vis));
int j,i,ok=0;
int move[4][2]={0,1,0,-1,1,0,-1,0};
queue<node>q;
node beg,end;
beg.x=x1;
beg.y=y1;
beg.step=0;
q.push(beg);
while(!q.empty())
{
end=q.front();
q.pop();
if(map[end.x][end.y]=='@')//遇见@则表示到达终点
{
if(p==1)
bu1[end.x][end.y]=end.step;
else
bu2[end.x][end.y]=end.step;
}
for(i=0;i<4;i++)
{
beg.x=end.x+move[i][0];
beg.y=end.y+move[i][1];
if(!vis[beg.x][beg.y]&&0<=beg.x&&beg.x<n&&0<=beg.y&&beg.y<m&&map[beg.x][beg.y]!='#')
{
vis[beg.x][beg.y]=1;
beg.step=end.step+11;
q.push(beg);
}
}
}
}
int main()
{
int sum,j,i,t,k,x1,x2,y1,y2,min;
int s[11000];
while(scanf("%d%d",&n,&m)!=EOF)
{ for(i=0;i<n;i++)
{
scanf("%s",map[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(map[i][j]=='Y')
{
x1=i;y1=j;
}
else if(map[i][j]=='M')
{
x2=i;y2=j;
}
}
}
memset(bu1,INF,sizeof(bu1));
bfs(x1,y1,1);
memset(bu2,INF,sizeof(bu2));
bfs(x2,y2,2);
min=INF;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(bu1[i][j]!=INF&&bu2[i][j]!=INF)
{
min=MIN(bu1[i][j]+bu2[i][j],min);//取两者步数和的最小值
}
}
}
printf("%d\n",min);
}
return 0;
}

  

hdoj 2612 Find a way【bfs+队列】的更多相关文章

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

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

  2. HDU 2612 - Find a way - [BFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...

  3. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  4. HDU 2612 Find a way bfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...

  5. BFS 队列

    Plague Inc. is a famous game, which player develop virus to ruin the world. JSZKC wants to model thi ...

  6. 农夫过河 (BFS)(队列)

    1 .问题描述 要求设计实现农夫过河问题(农夫带着一只狼,一只养,一棵白菜,一次只能带一个东西)如何安全过河. 2 .问题的解决方案: 可以用栈与队列.深度优先搜索算法及广度优先搜索算法相应的原理去解 ...

  7. 拆边+BFS队列骚操作——cf1209F

    这个拆边+队列操作实在是太秒了 队列头结点存的是一个存点集的vector,1到这个点集经过的路径权值是一样的,所以向下一层拓展时,先依次走一遍每个点的0边,再走1边...以此类推,能保证最后走出来的路 ...

  8. POJ——3278 Catch That Cow(BFS队列)

    相比于POJ2251的三维BFS,这道题做法思路完全相同且过程更加简单,也不需要用结构体,check只要判断vis和左右边界的越界情况就OK. 记得清空队列,其他没什么好说的. #include< ...

  9. HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

    Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...

随机推荐

  1. JLOI 2013 卡牌游戏

    问题描述: N个人坐成一圈玩游戏.一开始我们把所有玩家按顺时针从1到N编号.首先第一回合是玩家1作为庄家.每个回合庄家都会随机(即按相等的概率)从卡牌堆里选择一张卡片,假设卡片上的数字为X,则庄家首先 ...

  2. iostream/fstream中的输入输出流指针的绑定,tie函数的使用。

      为了兼容c语言的输入输出,c++里面采用tie将输入输出流经行绑定,所以cin/cout并不是独立的.当执行cin时,cout同时会被执行.反之亦然. by defalut,cin is tied ...

  3. 数据结构---顺序表(C++)

    顺序表 是用一段地址连续的存储单元依次存储线性表的数据元素. 通常用一维数组来实现 基本操作: 初始化 销毁 求长 按位查找 按值查找 插入元素 删除位置i的元素 判空操作 遍历操作 示例代码: // ...

  4. 关于 Java 性能监控您不知道的 5 件事,第 1 部分

    责怪糟糕的代码(或不良代码对象)并不能帮助您发现瓶颈,提高 Java? 应用程序速度,猜测也不能帮您解决.Ted Neward 引导您关注 Java 性能监控工具,从5 个技巧开始,使用Java 5 ...

  5. DOM in Angular2

    <elementRef> import {ElementRef} from "@angular/core"; constructor(private element:  ...

  6. IOS ITunesConnect 修改开发商名称

    ItunesConnect→ManagerApps→应用→Contact Us

  7. Druid数据库连接池两种简单使用方式

    阿里巴巴推出的国产数据库连接池,据网上测试对比,比目前的DBCP或C3P0数据库连接池性能更好 简单使用介绍 Druid与其他数据库连接池使用方法基本一样(与DBCP非常相似),将数据库的连接信息全部 ...

  8. http://www.ruanyifeng.com/blog/2011/09/restful

    http://www.ruanyifeng.com/blog/2011/09/restful

  9. CodeChef A

    问题是求出斐波那契数列的第n个,这里要用大数加法预处理,然后就可以了 代码: #include <iostream> #include <sstream> #include & ...

  10. 166. Fraction to Recurring Decimal

    题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...