HDU——2612Find a way(多起点多终点BFS)

本文介绍了一个名为 Findaway 的最短路径算法问题,该问题要求找出两个角色从初始位置到同一目标位置(KFC)的最短总时间。通过使用广度优先搜索策略,算法有效地计算了所有可能路径中的最小总时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Find a way

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

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
 

 

 

主要看起点和终点那种点少,以少的为起点开搜。1A水过

 

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
typedef long long LL;
const double PI=acos(-1.0);
const int N=210;
char pos[N][N];
int vis[N][N];
int n,m;
struct info
{
	int x;
	int y;
	int time;
	bool operator<(const info &a)
	{
		return time>a.time;
	}
};
vector<info>kfc;
int total[2][200][200];
info direct[4]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}},S,T;
inline info operator+(const info &a,const info &b)
{
	info c;
	c.x=a.x+b.x;
	c.y=a.y+b.y;
	return c;
}
void init()
{
	MM(pos);
	MM(vis);
	MMINF(total);
	kfc.clear();
}
inline bool check(const info &a)
{
	return (a.x>=0&&a.x<n&&a.y>=0&&a.y<m&&!vis[a.x][a.y]&&pos[a.x][a.y]!='#');
}
queue<info>Q;
int main(void)
{
	int i,j,cnt;
	while (~scanf("%d%d",&n,&m))
	{
		init();
		cnt=0;
		for (i=0; i<n; i++)
		{
			for (j=0; j<m; j++)
			{
				cin>>pos[i][j];
				if(pos[i][j]=='Y')
				{
					S.x=i;
					S.y=j;
					S.time=0;
				}
				else if(pos[i][j]=='M')
				{
					T.x=i;
					T.y=j;
					T.time=0;
				}
				else if(pos[i][j]=='@')
				{
					info k;
					k.x=i;
					k.y=j;
					kfc.push_back(k);
					cnt++;
				}
			}
		}
		int c=0;
		while (!Q.empty())
			Q.pop();
		Q.push(S);
		vis[S.x][S.y]=1;
		while (!Q.empty())
		{
			info now=Q.front();
			Q.pop();
			if(pos[now.x][now.y]=='@')
			{
				total[0][now.x][now.y]=now.time;
				if(++c==cnt)
					break;
			}	
			for (i=0; i<4; i++)
			{
				info v=now+direct[i];
				if(check(v))
				{
					v.time=now.time+11;
					vis[v.x][v.y]=1;
					Q.push(v);
				}				
			}
		}
		while (!Q.empty())
			Q.pop();
		MM(vis);
		c=0;
		Q.push(T);
		while (!Q.empty())
		{
			info now=Q.front();
			Q.pop();
			if(pos[now.x][now.y]=='@')
			{
				total[1][now.x][now.y]=now.time;
				if(++c==cnt)
					break;
			}	
			for (i=0; i<4; i++)
			{
				info v=now+direct[i];
				if(check(v))
				{
					v.time=now.time+11;
					vis[v.x][v.y]=1;
					Q.push(v);
				}				
			}
		}
		int r=INF;
		for (i=0; i<cnt; i++)
			r=min(r,total[0][kfc[i].x][kfc[i].y]+total[1][kfc[i].x][kfc[i].y]);
		printf("%d\n",r);
	}
	return 0;
}

 

转载于:https://www.cnblogs.com/Blackops/p/5766306.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值