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

相关推荐

点赞 评论 收藏
分享
球球别再泡了:坏,我单9要了14
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务