bsf find a way

Submit Status

Description

Input

Output

Sample Input

Sample Output

Hint

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
感谢汤神 我的一直 wa 汤神不辞幸苦帮我写了一个 真的是 万分感谢
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#define MAX 0x3f3f3f3f
using namespace std;
char map[205][205];
struct node
{
	int x;
	int y;
	int step;
};
int n, m;
int dis[205][205];
int nextd[4][2] = { 0,1,0,-1,1,0,-1,0 };
void bfs(int x, int y)
{
	bool vis[205][205];
	memset(vis, false, sizeof(vis));
	queue<node>q;
	node t, s;
	vis[x][y] = true;
	s.x = x;
	s.y = y;
	s.step = 0;
	q.push(s);
	while (!q.empty())
	{
		s = q.front();
		q.pop();
		if (map[s.x][s.y] == '@')
		{
			if (dis[s.x][s.y] == MAX)
				dis[s.x][s.y] = s.step;
			else
				dis[s.x][s.y] += s.step;
		}
		for (int i = 0; i < 4; i++)
		{
			t.x = s.x + nextd[i][0];
			t.y = s.y + nextd[i][1];
			t.step = s.step + 1;
			if (t.x < 0 || t.x >= n || t.y < 0 || t.y >= m) continue;
			if (vis[t.x][t.y]) continue;
			if (map[t.x][t.y] == '#') continue;
			vis[t.x][t.y] = true;
			q.push(t);
		}
	}
}

int main()
{
	node M, Y;
	int i, j;
	while (~scanf("%d %d", &n, &m))
	{
		memset(dis, 0x3f, sizeof(dis));
		for (i = 0; i < n; i++) scanf("%s", map[i]);
		for (i = 0; i < n; i++)
		{
			for (j = 0; j < m; j++)
			{
				if (map[i][j] == 'M')
				{
					M.x = i;
					M.y = j;
				}
				if (map[i][j] == 'Y')
				{
					Y.x = i;
					Y.y = j;
				}
			}
		}
		bfs(M.x, M.y);
		bfs(Y.x, Y.y);
		int min = MAX;
		for (i = 0; i < n; i++)
		{
			for (j = 0; j < m; j++)
			{
				if (min <= dis[i][j]) continue;
				min = dis[i][j];
			}
		}
		printf("%d\n", min*11);
	}
	return 0;
}
by  swust.acm tzc
全部评论

相关推荐

牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
程序员牛肉:主要是因为小厂的资金本来就很吃紧,所以更喜欢有实习经历的同学。来了就能上手。 而大厂因为钱多,实习生一天三四百的就不算事。所以愿意培养你,在面试的时候也就不在乎你有没有实习(除非是同级别大厂的实习。) 按照你的简历来看,同质化太严重了。项目也很烂大街。 要么换项目,要么考研。 你现在选择工作的话,前景不是很好了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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