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




全部评论

相关推荐

个人背景:学院二本计科专业&nbsp;大二开始实习个人经历:安克创新&nbsp;、理想汽车、字节跳动碎碎念:我做事只有三分钟热度。看到进了大厂的同学,我会羡慕,也会跟着努力上进;但遇到好看的小说,我又会放下手头的事沉迷其中,之前的坚持也就中断了。我有些自卑,总觉得自己学历和外貌都不够好。之前偶然在网上受到关注,我就喜欢上了上网,因为这里有很多人认可我。但我也很在意别人的评价,偶尔看到嘲讽的言论,会触发我的自卑情绪,让我感到愤怒。有时候我会强硬地回怼,有时候又会懦弱地选择无视。我也有虚荣心。不管是拿到安克、理想还是字节的机会,我在分享的时候都会带着这份心思。我会特意强调自己学历不好,是为了衬托出过程的艰难,以此显得自己更厉害。我知道,人往往会炫耀自己缺少的东西,来掩盖内心的空洞。我总想着走捷径,不太喜欢踏踏实实地做事。找实习的时候,我花了更多时间在研究面试技巧上,而不是提升专业能力。我会反复听面试录音分析技巧,看面试教程学习怎么和不同的面试官沟通,还会每天自言自语练习语言表达,同学都觉得我有点奇怪。我的实习生涯里,侥幸和运气占了很大一部分。我总在想,如果有一天我失去了这份幸运,这些特质可能会让我一蹶不振。ps:&nbsp;很多人会问我学习路线和经验&nbsp;但是就像我上面说的&nbsp;我的实习过程靠的很多是关键节点的运气&nbsp;技术上面我可能不如很多人&nbsp;&nbsp;所以请大家理性求助和理性参考我的回答&nbsp;附上我的投递记录
我的offer在哪里...:从去年看到现在,飞升哥就是榜样
我的求职进度条
点赞 评论 收藏
分享
04-27 15:01
早稲田大学 Java
牛客72191338...:可能是时间点的问题,四月底机会确实会相对少点,但佬这个学历摆在这,会有机会的
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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