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

相关推荐

不愿透露姓名的神秘牛友
07-07 12:04
毕业生招你惹你了,问一个发薪日来一句别看网上乱七八糟的你看哪个工作没有固定发薪日扭头就取消了面试就问了一句公司都是这个态度吗还搞上人身攻击了...
程序员小白条:呃呃呃,都还没面试,我都不会问这么细,何况通不通过,去不去都另说,你没实力和学历的话,在外面就这样,说实话没直接已读不回就不错了,浪费时间基本上
点赞 评论 收藏
分享
酷酷我灵儿帅:这去不去和线不线下面说实话没啥关系
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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