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




全部评论

相关推荐

大疆在线测评都考什么呀,会考企业概况啥的吗
又被画饼了的做题家很...:不会。刚做完,就是材料分析、态度题、算术题、逻辑题。总共60道。
投递大疆等公司7个岗位
点赞 评论 收藏
分享
06-24 19:27
云南大学 Java
点赞 评论 收藏
分享
05-19 15:21
已编辑
门头沟学院 Java
白火同学:你才沟通了200,说实话,北上广深杭这里面你连一座城市的互联网公司都没投满呢,更别说还有各种准一线二线城市了。等你沟通突破了三位数,还没结果再考虑转行的事吧。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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