N - Find a way HDU - 2612(两次BFS)

N - Find a way HDU - 2612

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

题意:两个人想去肯德基碰面,但有好多家肯德基,找出离两个人都很近的那家肯德基。

思路:先遍历一个人去所有肯德基需要的时间,存到dis数组里; 再遍历另一个人用+=更新dis数组。找到所有dis肯德基的最小值。

代码:

#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

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

char a[204][204];
int via[204][204];
int dis1[204][204];
int n,m;
ty t,d;

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

    while ( !Q.empty() ) {
        t = Q.front();
        Q.pop();

        if ( a[t.x][t.y]=='@' ) {  // 只在肯德基的地方存dis,减少不必要的运算。
            dis1[t.x][t.y] += t.step;
        }

        for ( int 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 ( a[d.x][d.y]!='#' && via[d.x][d.y]==0 ) {
                Q.push(d);
                via[d.x][d.y] = 1;
            }
        }
    }
}

int main()
{
    int yx,yy,mx,my,i,j;
    while ( cin>>n>>m ) {
        for ( i=0; i<n; i++ ) {
            for ( j=0; j<m; j++ ) {
                cin >> a[i][j];
                if ( a[i][j]=='Y' ) {
                    yx = i;
                    yy = j;
                }
                if ( a[i][j]=='M' ) {
                    mx = i;
                    my = j;
                }
            }
        }
        memset(via,0,sizeof(via));
        memset(dis1,0,sizeof(dis1));
        bfs(yx,yy);

        memset(via,0,sizeof(via));
        bfs(mx,my);

        int minn = 0x3f3f3f;
        for ( i=0; i<n; i++ ) {
            for ( j=0; j<m; j++ ) {
                if ( dis1[i][j]!=0 && dis1[i][j]<minn ) {  // dis为0就代表此点不是kfc
                    minn = dis1[i][j];
                }
            }
        }
        cout << 11*minn << endl; // 走一步的时间为11
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值