hdu 2616 Find a way

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.

题意:求两个点Y和M 到同一个@最短的距离之和。 bfs求最短路问题,分别以两个点为原点,求出每一个@相对于两个点的最短距离dY和dM,再找出最小的dY + dM;

#include "iostream"
#include "queue"
#include "algorithm"
#include "cstring"

using namespace std;


char city[205][205]; //输入矩阵
int visit[205][205],d1[205][205],d2[205][205],d[205]

int stepArr[4][2] = {{1,0},{-1,0},{0,-1},{0,1}} ;//方向
int m,n;//矩阵范围


struct Node{
    int x;
    int y;
    int step;
    Node(int a,int b,int c):x(a),y(b),step(c){}

};

void bfs(int r,int c){// 点city[r][c]为原点宽搜
    Node s(r,c,0);

    memset(visit,0,sizeof(visit));
    queue<Node> q;
    while(!q.empty()) q.pop();
    q.push(s);

    while(!q.empty()) {
        s = q.front();
        q.pop();
        d1[s.x][s.y] = s.step;//记录与原点的距离,用d1记录
        visit[s.x][s.y] = 1;

        for(int i = 0; i < 4; i++) {
            int x = s.x + stepArr[i][0];
            int y = s.y + stepArr[i][1];

            if(x >= 0 && x < m && y >= 0 && y < n && !visit[x][y] && city[x][y] != '#') {
                Node next(x,y,s.step+1);
                visit[x][y] = 1;
                q.push(next);
            }
        }

    }
}

int main() {

    while(cin >> m >> n) {
            int r1,c1,r2,c2;
        for(int i = 0; i < m; i++)
            for(int j = 0;j < n; j++) {
                cin >> city[i][j];
                if(city[i][j] == 'Y') {
                    r1 = i;
                    c1 = j;
                }
                if(city[i][j] == 'M') {
                    r2 = i;
                    c2 = j;
                }

            }

        memset(d1,-1,sizeof(d1));
        memset(d2,-1,sizeof(d2));
        memset(d,0,sizeof(d));

        bfs(r2,c2);  //以M点为原点bfs,此时距离储存在d1中

        //将d1中数据对应存在d2中,方便第二次dfs
        for(int i = 0; i < m; i++)
            for(int j = 0;j < n; j++) d2[i][j] = d1[i][j];
        bfs(r1,c1);//第二次bfs,此时d1中数据为Y点与各点的距离

        int tot = 0;
        for(int i = 0; i < m; i++)
            for(int j = 0;j < n; j++) {
                if(city[i][j] == '@' && d1[i][j] >= 0 && d2[i][j] >=0) d[tot++] = d1[i][j] + d2[i][j];
            }

        int ans = d[0];
        for(int i = 1; i < tot; i++) {
            ans = min(ans,d[i]);
        }

        cout << ans * 11 << endl;
    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值