Find a way

来源:互联网 发布:音乐盒淘宝 编辑:程序博客网 时间:2024/06/06 03:23
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 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
 

Sample Output
668866
 

 Y,M两个人到达同一个KFC所花的最少时间,两遍BFS,最后更迭出最小时间和


 
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <queue>using namespace std;const int N = 250;struct node{    int x, y, step;};int n, m;char map[N][N];int vy[N][N], vm[N][N];         //visited,表示当前位置是否入队int ty[N][N], tm[N][N];         //time,表示Y和M所能到达的KFC的所有时间int xx[4] = {1, 0, 0, -1};int yy[4] = {0, 1, -1, 0};void BFS(int xi, int yj, int vis[N][N], int tt[N][N]){    queue<node> q;    struct node t, u;    t.x = xi;    t.y = yj;    t.step = 0;    q.push(t);    vis[xi][yj] = 1;    while(!q.empty())    {        u = q.front();        q.pop();        if(map[u.x][u.y] == '@')        {            tt[u.x][u.y] = u.step;            vis[u.x][u.y] = 1;        }        for(int k = 0; k < 4; k++)        {            t.x = u.x + xx[k];            t.y = u.y + yy[k];            if(t.x >= 0 && t.x < n && t.y >= 0 && t.y < m)            {                if(map[t.x][t.y] != '#' && !vis[t.x][t.y])                {                                        t.step = u.step + 1;                                       q.push(t);                    vis[u.x+xx[k]][u.y+yy[k]] = 1;                }            }        }    }}int main(){    while(~scanf("%d%d", &n, &m))    {        int yi, yj, mi, mj;        for(int i = 0; i < n; i++)        {                       scanf("%s", map[i]);            for(int j = 0; j < m; j++)            {                if(map[i][j] == 'Y')                {                    yi = i;                    yj = j;                }                else if(map[i][j] == 'M')                {                    mi = i;                    mj = j;                }            }        }  //全部初始化        memset(ty, 0, sizeof(ty));        memset(tm, 0, sizeof(tm));        memset(vy, 0, sizeof(vy));        memset(vm, 0, sizeof(vm));        BFS(yi, yj, vy, ty);        BFS(mi, mj, vm, tm);        int cnt = 1e7;          //最少时间        for(int i = 0; i < n; i++)            for(int j = 0; j < m; j++)            {                if(map[i][j] == '@' && vy[i][j] && vm[i][j])  //同一个KFC,且两者同时访问过                {                    cnt = min(cnt, ty[i][j] + tm[i][j]);                }            }        printf("%d\n", 11*cnt);    }    return 0;}

原创粉丝点击