Find a way

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

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int vis[1001][1001];
int n, m;
struct node
{
    int x,y;
    int step;
};
char map[205][205];
int ystep[205][205];
int mstep[205][205];
int INF = 1000001;
int dx[4] = { 1, -1, 0, 0 };
int dy[4] = { 0, 0, 1, -1 };
void bfs(int x, int y, int flag) {
    queue<node>q;
    node first, now;
    first.x = x;
    first.y = y;
    first.step = 0;
    vis[first.x][first.y] = 1;
    q.push(first);
    while (!q.empty()) {
        first = q.front();
        q.pop();
        now.step = first.step + 1;
        for (int i = 0; i < 4; i++) {
            int xx = first.x + dx[i];
            int yy = first.y + dy[i];
            if (vis[xx][yy] == 1 || xx < 0 || xx >= n || yy < 0 || yy >= m || map[xx][yy] == '#')
                continue;
            vis[xx][yy] = 1;
            now.x = xx;
            now.y = yy;
            if (map[xx][yy] == '@'&&flag == 0) {
                mstep[xx][yy] = now.step;
            }
            else if (map[xx][yy] == '@'&&flag == 1) {
                ystep[xx][yy] = now.step;
            }
            q.push(now);
        }
    }

}
int main() {
    while (cin >> n >> m) {
        int min = 1000001;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                ystep[i][j] = INF;
                mstep[i][j] = INF;
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cin >> map[i][j];
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (map[i][j] == 'Y') {
                    memset(vis, 0, sizeof(vis));
                    bfs(i, j, 0);
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (map[i][j] == 'M') {
                    memset(vis, 0, sizeof(vis));
                    bfs(i, j, 1);
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (map[i][j] == '@'&&min > mstep[i][j] + ystep[i][j]) {
                    min = mstep[i][j] + ystep[i][j];
                }
            }
        }
        cout << min * 11 << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yihanyifan/article/details/81206408
今日推荐