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
全部评论

相关推荐

明明就不饿:看不懂你到底会啥,什么岗位
点赞 评论 收藏
分享
找工作勤劳小蜜蜂:自我描述部分太差,完全看不出想从事什么行业什么岗位,也看不出想在哪个地区发展,这样 会让HR很犹豫,从而把你简历否决掉。现在企业都很注重员工稳定性和专注性,特别对于热爱本行业的员工。 你实习的工作又太传统的it开发(老旧),这部分公司已经趋于被淘汰,新兴的互联网服务业,比如物流,电商,新传媒,游戏开发和传统的It开发有天然区别。不是说传统It开发不行,而是就业岗位太少,基本趋于饱和,很多老骨头还能坚持,不需要新血液。 工作区域(比如长三角,珠三角,成渝)等也是HR考虑的因素之一,也是要你有个坚定的决心。否则去几天,人跑了,HR会被用人单位骂死。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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