HDU - Find a way(BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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

Problem solving report:

Description: 两人分别从"Y"和"M"出发,在地图上走,要找一个"@"见面,每走一格花费时间11分钟,求最快的见面时间。
Problem solving: 以两个人分别为起点做BFS,求出两人到每家KFC的各自花费的时间。对全部KFC维护两人到达时间之和的最小值即为答案。

Accepted Code:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 205;
const int inf = 0x3f3f3f3f;
char mp[MAXN][MAXN];
bool vis[MAXN][MAXN];
int r, c, cnt, min_, t[2][MAXN * MAXN];
int dir[][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
map <pair <int, int>, int> KFC;
struct edge {
    int x, y, t;
    edge(int x_, int y_, int t_) : x(x_), y(y_), t(t_) {}
}Y(0, 0, 0), M(0, 0, 0);
void BFS(edge e, int s) {
    int tx, ty;
    queue <edge> Q;
    Q.push(e);
    memset(vis, 0, sizeof(vis));
    vis[e.x][e.y] = 1;
    while (!Q.empty()) {
        edge p = Q.front();
        Q.pop();
        if (mp[p.x][p.y] == '@')
            t[s][KFC[make_pair(p.x, p.y)]] = p.t;
        for (int i = 0; i < 4; i++) {
            tx = p.x + dir[i][0];
            ty = p.y + dir[i][1];
            if (tx >= 0 && tx < r && ty >= 0 && ty < c && mp[tx][ty] != '#' && !vis[tx][ty]) {
                vis[tx][ty] = 1;
                Q.push(edge(tx, ty, p.t + 1));
            }
        }
    }
}
int main() {
    while (~scanf("%d%d", &r, &c)) {
        KFC.clear();
        cnt = 0, min_ = inf;
        memset(t, 0x3f, sizeof(t));
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                scanf(" %c", &mp[i][j]);
                if (mp[i][j] == 'Y')
                    Y = edge(i, j, 0);
                else if (mp[i][j] == 'M')
                    M = edge(i, j, 0);
                else if (mp[i][j] == '@')
                    KFC[make_pair(i, j)] = cnt++;
            }
        }
        BFS(Y, 0), BFS(M, 1);
        for (int i = 0; i < cnt; i++)
            min_ = min(min_, t[0][i] + t[1][i]);
        printf("%d\n", min_ * 11);
    }
    return 0;
}
全部评论

相关推荐

来,说点可能被同行“骂”的大实话。🙊当初接数字马力Offer时,朋友都说:“蚂蚁的“内包”公司?你想清楚啊!”但入职快一年后的今天,我反而对他有了不一样的看法!🔹&nbsp;是偏见?还是信息差!之前没入职之前外面都在说什么岗位低人一等这类。实际上:这种情况不可至否,不能保证每个团队都是其乐融融。但我在的部门以及我了解的周边同事都还是十分好相处的~和蚂蚁师兄师姐之间也经常开一些小玩笑。总之:身份是蚂蚁公司给的,地位是自己挣的(一个傲娇女孩的自述)。🔹&nbsp;待遇?玩的就是真实!试用期工资全额发!六点下班跑得快(早9晚6或者早10晚7,动态打卡),公积金顶格交。别听那些画饼的,到手的钱和下班的时间才是真的(都是牛马何必难为牛马)。🔹&nbsp;能不能学到技术?来了就“后悔”!我们拥有权限直通蚂蚁知识库,技术栈多到学不完。说“学不到东西”的人,来了可能后悔——后悔来晚了(哈哈哈哈,可以不学但是不能没有)!💥&nbsp;内推地址:https://app.mokahr.com/su/ueoyhg❗我的内推码:NTA6Nvs走我的内推,可以直达业务部门,面试流程更快速,进度可查!今天新放HC,之前挂过也能再战!秋招已经正式开始啦~机会就摆在这,敢不敢来试一试呢?(和我一样,做个勇敢的女孩)
下午吃泡馍:数字马力的薪资一般哇,5年经验的java/测试就给人一万出头,而且刚入职第三天就让人出差,而且是出半年
帮你内推|数字马力 校招
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务