HDU - 2612 Find a way

Find a way

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

题目大意,两个人很久没见面了,准备找个KFC见一面,要求输出最短路径,两个人所走路乘11;

思路:两个人分别BFS用一个数组记录他们抵达的时间,然后比较大小,找出和最小的,但是要注意,一但有一个元素是0就代表一个人无法到达,这个数据无法使用

提交链接HDU-2612

#include <stdio.h>
#include <string.h>

int n, m, tx, ty, mx, my, k[100000][4], ans, min, max, flag[100000];
char map[201][201];
int vis[201][201];
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

struct node {
    int x;
    int y;
    int step;
}list[100000];

void bfs(int a, int b, int c);
int go(int x, int y);

int main(void)
{
    while(scanf("%d%d", &n, &m) != EOF){
    ans = 0;
    memset(map, '#', sizeof(map));
        memset(k, 0, sizeof(k));
    for (int i = 0; i < n; i++){
        getchar();
        for (int j = 0; j < m; j++){
            scanf("%c", &map[i][j]);
            if (map[i][j] == 'Y'){
                tx = i;
                ty = j;
            }
            else if (map[i][j] == 'M'){
                mx = i;
                my = j;
            }
            else if(map[i][j] == '@'){
                k[ans][0] = i;
                k[ans][1] = j;
                ans++;
            }
        }
    }
    bfs(tx, ty, 2);
    bfs(mx, my, 3);
    min = 10000000;
    for (int i = 0; i < ans; i++){
        if (k[i][2] + k[i][3] < min && k[i][2] != 0 && k[i][3] != 0){
            min = k[i][2] + k[i][3];
        }
    }
    printf("%d\n", min * 11);
    }
    return 0;
}

int go(int x, int y)
{
    if (x < 0 || x >= n || y < 0 || y >= m || map[x][y] == '#' || map[x][y] == 'M' || map[x][y] == 'Y')
        return 0;
    return (!vis[x][y]);
}

void bfs(int a, int b, int c)
{
    int sum, head = 0, tail = 1, m = 0;
    int x, y, xx, yy;
    memset(list, 0, sizeof(list));
    memset(flag, 0, sizeof(flag));
    list[0].x = a;
    list[0].y = b;
    list[0].step = 0;
    memset(vis, 0, sizeof(vis));
    while (head < tail){
        x = list[head].x;
        y = list[head].y;
        sum = list[head].step;
        for (int i = 0; i < 4; i++){
            xx = x + dir[i][0];
            yy = y + dir[i][1];
            if (go(xx, yy)){
                if (map[xx][yy] == '@'){
                    for (int i = 0; i < ans; i++)
                        if (k[i][0] == xx && k[i][1] == yy && flag[i] == 0){
                            k[i][c] = sum + 1;
                            m++;
                            flag[i] = 1;
                            break;
                        }
                }
                vis[xx][yy] = 1;
                list[tail].x = xx;
                list[tail].y = yy;
                list[tail].step = sum + 1;
                tail++;
            }
        }
        if (m >= ans){
            break;
        }
        head++;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值