Bfs广搜 HDU - 2612

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12638    Accepted Submission(s): 4060



Problem 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. #...#
 

Sample Output
  
  
66 88 66
 
题意:'@'是肯德基,求Y和M到肯德基的最短时间和。
分别对Y和M进行两次bfs,记录到每一个点的时间,然后比较就行。。。挺水的。。。

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

using namespace std;
char ch[220][220];
int vist[220][220];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int n, m;

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

int step_y[220][220], step_m[220][220];

int bfs(int sx, int sy, int NO) {
    queue<node> que;
    node p, next;
    p.step = 0, p.x = sx, p.y = sy;
    que.push(p);
    while(!que.empty()) {
        p = que.front();
        que.pop();
        if(ch[p.x][p.y] == '@') {
            if(NO == 1)
                step_y[p.x][p.y] = p.step;
            else
                step_m[p.x][p.y] = p.step;
        }
        for(int i = 0; i < 4; i++) {
            int nx = p.x + dx[i];
            int ny = p.y + dy[i];
            if(nx>=0 && nx<n && ny>=0 && ny<m && ch[nx][ny]!='#' && !vist[nx][ny]) {
                next.step = p.step+1;
                next.x = nx;
                next.y = ny;
                que.push(next);
                vist[nx][ny] = 1;
            }
        }
    }

}
int main()
{
    int xy, yy, xm, ym;
    while(~scanf("%d%d", &n, &m)) {
        for(int i = 0; i < n; i++) {
            getchar();
            for(int j = 0; j < m; j++) {
                scanf("%c", &ch[i][j]);
                if(ch[i][j] == 'Y') {
                    xy = i;
                    yy = j;
                } else if(ch[i][j] == 'M') {
                    xm = i;
                    ym = j;
                }
            }
        }
        memset(vist, 0, sizeof(vist));
        memset(step_y, 0, sizeof(step_y));
        vist[xy][yy] = 1;
        bfs(xy, yy, 1);

        memset(vist, 0, sizeof(vist));
        memset(step_m, 0, sizeof(step_m));
        vist[xm][ym] = 1;
        bfs(xm, ym, 2);

        int minn = 1e9;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(ch[i][j] == '@' && step_m[i][j] != 0 && step_y[i][j] != 0) {
                    minn = min(minn, step_m[i][j]+step_y[i][j]);
                }
            }
        }
        long long ans = minn*11;
        printf("%lld\n", ans);
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值