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. QR code 乱谈(一)

    缘由 促使草人写这一系列(将会是)文章的原因是二维码现在很流行,很容易接触到,而且二维码又是那么容易就生成——就不说有很多在线的生成器,许多应用软件也都有生成二维码的功能,比如Firefox浏览器.Q ...

  2. 对于数组使用sizeof(a)和使用sizeof(a[0])

    #include "stdafx.h" #include <iostream> using namespace std; int main() { ]={}; cout ...

  3. codeforces 395B2 iwiwi

    #include<cstdio> #include<cstring> using namespace std; ]="iiiiiiiiiiiiiiiiiiiiii&q ...

  4. HDU 1754 I Hate It(线段树)

    点我看题目 题意  :又是一道中问题,我就不说题意了.... 思路 : 线段树,这道题跟1166差不多,改一些地方就差不多了. #include <iostream> #include & ...

  5. ffmpeg编译 --enable :没有命令

    参照官方推荐的编译:http://www.roman10.net/how-to-build-ffmpeg-with-ndk-r9/ build_config.sh总是不过, 问题原因:./config ...

  6. Jmeter java协议配置文件导入

    一. 方法一 通过ClassLoader获取当前路径,想在java脚本里读取druid.properties,路径如下 apache-jmeter --bin --druid.properties 相 ...

  7. *[topcoder]TheTree

    http://community.topcoder.com/stat?c=problem_statement&pm=12746&rd=15703 这道题有意思.给了树的根和每层节点的个 ...

  8. webbench的详细使用

    webbench是什么?是一款相当给力的网站压力测试工具.(优点自行搜索) 使用webbench需要两大步骤: 1.安装webbench 2.熟悉webbench命令 一.安装webbench 1.下 ...

  9. ANDROID_MARS学习笔记_S01原始版_009_下载文件

    一.代码1.xml(1)main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...

  10. 在ubuntu系统荣品开发配套JDK安装

    chmod 755 jdk-6u29-linux-i586.bin ./jdk-6u29-linux-i586.bin