Find a way

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

题意:两个小伙伴在不同的地方,想要去KFC见面,现在有很多家KFC,问到哪家KFC能使他们的步数和最少。

正确的思路: 很明显最短路径要用bfs来做,这里需要两次bfs得到的距离加起来,即在bfs的基础上定义dis[102][102] 来储存距离。

错误的思路:开始想要对每个KFC都进行bfs直接找到两个小伙伴的距离,再排序,结果超时了。

给出代码

#include <bits/stdc++.h>

using namespace std;

char Map[202][202]; // 储存地图
int via[202][202]; // 是否来过
int dis[202][202]; // 储存距离
int n,m;

typedef struct node {
    int x,y,step;
}ty;
ty t,d;

void bfs( int x, int y )
{
    int next[4][2] = {
        {0,1},{0,-1},{1,0},{-1,0}
    };
    int i,j;
    via[x][y] = 1;
    queue <ty> Q;
    t.x = x;
    t.y = y;
    t.step = 0;
    Q.push(t);
    while ( !Q.empty() ) {
        t = Q.front();
        Q.pop();

        if ( Map[t.x][t.y]=='@' ) {
            dis[t.x][t.y] += t.step; // 这里是 += 因为是两个人的步数合。
        }

        for ( i=0; i<4; i++ ) {
            d.x = t.x + next[i][0];
            d.y = t.y + next[i][1];
            d.step = t.step + 1;
            if ( d.x<0 || d.y<0 || d.x>=n || d.y>=m ) {
                continue ;
            }
            if ( Map[d.x][d.y]!='#' && via[d.x][d.y]==0 ) {
                via[d.x][d.y] = 1;
                Q.push(d);
            }
        }
    }

}

int main()
{
    int i,j,x1,x2,y1,y2;
    while ( cin>>n>>m ) {
        for ( i=0; i<n; i++ ) {
            for ( j=0; j<m; j++ ) {
                cin >> Map[i][j];
                if ( Map[i][j]=='Y' ) {
                    x1 = i;
                    y1 = j;
                }
                if ( Map[i][j]=='M' ) {
                    x2 = i;
                    y2 = j;
                }
            }
        }

        memset(via,0,sizeof(via));
        memset(dis,0,sizeof(dis));
        bfs(x1,y1);
        memset(via,0,sizeof(via));
        bfs(x2,y2);

        int minn = 99999999;  // 这里好像也可以是0x3f 
        for ( i=0; i<n; i++ ) {
            for ( j=0; j<m; j++ ) {
                if ( dis[i][j]!=0 && dis[i][j]<minn ) {
                    minn = dis[i][j];
                }
            }
        }
        cout << 11*minn << endl; // 一步的时间是11min 所以输出结果乘以11
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值