HDU - 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): 22363    Accepted Submission(s): 7293


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
 

Author
yifenfei
 
            这道题就是有两个起点Y,M,想知道他们最短的路径和到达最近@是多少,其中#不能走;

            但是有一个点我要更加认真的反思下,以后绝对不能用0来初始化vis数组,非常容易会使不能到达的地方(0),失误的进行运算!!;

#include<iostream>
#include<cstdio>
#include<queue>
#include<utility>
using namespace std;
int m, n;
char map[205][205];
int fx[4][2] = { 0,1,1,0,-1,0,0,-1 };
int fir_vis[205][205];
int sec_vis[205][205];
void BFS(int x, int y, int vis[][205])
{
	vis[x][y] = 1;
	queue<pair<int, int>>q;
	q.push(make_pair(x, y));
	while (!q.empty())
	{
		pair<int,int>top = q.front();
		q.pop();
		for (int s = 0; s < 4; s++)
		{
			int li = top.first + fx[s][0];
			int ri = top.second + fx[s][1];
			if (li >= 0 && li < m&&ri >= 0 && ri < n&& !vis[li][ri]&&map[li][ri]!='#')
			{
				vis[li][ri] = vis[top.first][top.second] + 1;
				q.push(make_pair(li, ri));
			}
		}
	}
}
int main()
{
	while (cin >> m >> n)
	{
		memset(fir_vis, 0, sizeof(fir_vis));
		memset(sec_vis, 0, sizeof(sec_vis));
		for (int s = 0; s < m; s++)
		{
			for (int w = 0; w < n; w++)
			{
				cin >> map[s][w];
			}
		}
		for (int s = 0; s < m; s++)
		{
			for (int w = 0; w < n; w++)
			{
				if (map[s][w] == 'Y')
				{
					BFS(s, w, fir_vis);
				}
				if (map[s][w] == 'M')
				{
					BFS(s, w, sec_vis);
				}
			}
		}
		int ans = 0x3f3f3f3f;
		for (int s = 0; s < m; s++)
		{
			for (int w = 0; w < n; w++)
			{
				if (map[s][w] == '@'&&fir_vis[s][w]!=0&&sec_vis[s][w]!=0)
				{
					ans = min(ans, fir_vis[s][w] + sec_vis[s][w]);
				}
			}
		}
		cout << 11*ans - 22 << endl;
	}
	return 0;
}




全部评论

相关推荐

AI牛可乐:哇,听起来你很激动呢!杭州灵枢维度科技听起来很厉害呀~你逃课去白马培训,老冯会同意吗?不过既然你这么感兴趣,肯定是有原因的吧! 对了,想了解更多关于这家公司或者求职相关的问题吗?可以点击我的头像私信我哦,我可以帮你更详细地分析一下!
你都用vibe codi...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务