hdoj 2612 Find a way【bfs+队列】

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

[email protected]

.#...

.#...

@..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;
}

  

时间: 2024-12-19 20:38:00

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

zoj 1649 Rescue (bfs+队列)

Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want

HDU 1242 &amp;&amp; ZOJ 1649( BFS (队列 || 优先队列)).

~~~~ 突然发现一篇搜索的题目都有写.昨天发现道bfs题目,HDU上AC, ZOJ上WA.不得不说HDU上的数据之水.. 今天早起刷题有了思路,并用队列和单调队列都写了一遍,0MS飘过~~ ~~~~ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649 ~~~~ 首先有坑的地方是friends,对嘛,朋友有很多,ang

hdu 2102 A计划 (bfs+队列)

A计划 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9360    Accepted Submission(s): 2265 Problem Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老.

hdoj 2612 find a way (两次bfs)

Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6221    Accepted Submission(s): 2070 Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally.

hdoj 2102 A计划 【BFS】

题目:hdoj 2102 A计划点击打开链接 题意:中文的就不说了.求救出公主所需要的最短时间,所以用广搜. 分析:读题之后不难做,比一般的题目多了一个条件就是可以传送,那么我们可以在广搜里面加一个传送的条件就好了. 其次这个题目注意有个坑就是如果两边都是传送门的话也不行 还有注意广搜写法,如果把队列定义成全局的话注意清空!! #include <cstdio> #include <iostream> #include <queue> #include <cstr

HDU 2612 Find a way bfs 难度:1

http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两人最大时间最小才对 #include <cstdio> #include <cstring> #include <queue> using namespace std; const int inf=0x3fffffff; char maz[300][301]; int n,

hdoj 1885 Key Task 【BFS+状态压缩】

题目:hdoj 1885 Key Task 题意:给出一些点,然后有一些钥匙和门,钥匙拿到才可以打开门,问到出口的最短时间. 分析:很明显的广搜 + 状态压缩题目. 坑点: 1:题目没读清楚,以为要把所有的们打开才能出去. AC代码: #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include &

hdoj 1428 漫步校园 【BFS+DFS】

题目:hdoj 1428 漫步校园 分析:题意还是有必要说的,他考虑从A区域到B区域仅当存在一条从B到机房的路线比任何一条从A到机房的路线更近,注意这句话,可见先让你求每个点到机房(n,n)点的最短路.当然这里用BFS比较好,注意要用优先队列..接着这句话告诉你,每次选择走的时候可以有多种选择,只要满足上面每一步都比当前这一步更近,即dis[child] < dis[father],然后求到终点有多少条.(注意不是求最短路有多少种走法),这一步可以用记忆话深搜,用dp[i][j]表示从当前点到终

HDU2717:Catch That Cow(BFS 队列)

这是一道用队列实现的BFS基础搜索题,学长给我们加这道题主要是让我们联系数据结构里面的队列,话不多说看代码吧. #include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #include <queue> #include <stack> #define LL long long #define mem(a) memset(a,0,size