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




全部评论

相关推荐

无情咸鱼王的秋招日记之薛定谔的Offer:好拒信,偷了,希望有机会用到
点赞 评论 收藏
分享
10-28 11:04
已编辑
美团_后端实习生(实习员工)
一个2人:我说几个点吧,你的实习经历写的让人觉得毫无含金量,你没有挖掘你需求里的 亮点, 让人觉得你不仅打杂还摆烂。然后你的简历太长了🤣你这个实习经历看完,估计没几个人愿意接着看下去, sdk, 索引这种东西单拎出来说太顶真了兄弟,好好优化下简历吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务