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. poj 2828 Buy Tickets (线段树(排队插入后输出序列))

    http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissio ...

  2. [XJOI NOI2015模拟题13] A 神奇的矩阵 【分块】

    题目链接:XJOI NOI2015-13 A 题目分析 首先,题目定义的这种矩阵有一个神奇的性质,第 4 行与第 2 行相同,于是第 5 行也就与第 3 行相同,后面的也是一样. 因此矩阵可以看做只有 ...

  3. Codeforces Round #210

    A:简单题: #include<cstdio> using namespace std; int n,k; int main() { scanf("%d%d",& ...

  4. cxf2.4.3中jaxb-api.jar、jaxws-api.jar与jdk1.6.0_02不兼容问题

    http://chxiaowu.iteye.com/blog/1243475 Exception in thread "main" java.lang.NoClassDefFoun ...

  5. 了解实时媒体的播放(RTP/RTCP 和 RTSP)

    http://blog.csdn.net/span76/article/details/12913307 RTP/RTCP RTP是基于 UDP协议的, UDP不用建立连接,效率更高:但允许丢包, 这 ...

  6. JavaWeb学习总结(十三)——使用Session防止表单重复提交

    在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 一.表单重复提 ...

  7. 使用Mybatis Generator自动生成Mybatis相关代码

    本文将简要介绍怎样利用Mybatis Generator自动生成Mybatis的相关代码: 一.构建一个环境: 1. 首先创建一个表: CREATE TABLE pet (name VARCHAR(2 ...

  8. java.lang.Boolean为null时

    public class TestBooleanNull { public static void main(String[] args) { if (test()) { System.out.pri ...

  9. WinForm实现简单的拖拽功能(C#)

    用到了ListBox和TreeView两个控件,ListBox作为数据源,通过拖拽其中的数据放置到TreeView上,自动添加一个树节点 ListBox控件的MouseDown用于获取要拖拽的值并调用 ...

  10. hadoop namenode启动过程详细剖析及瓶颈分析

    NameNode中几个关键的数据结构 FSImage Namenode 会将HDFS的文件和目录元数据存储在一个叫fsimage的二进制文件中,每次保存fsimage之后到下次保存之间的所有hdfs操 ...