HDU 3612 Find a way【简单搜索bfs】

来源:互联网 发布:算法导论这本书怎么样 编辑:程序博客网 时间:2024/06/07 22:27

Find a way

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


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 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
 

Sample Output
668866
 

Author
yifenfei
 

Source
奋斗的年代
 

Recommend
yifenfei


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612


说明:本题借鉴了讨论区答案。


题意:Y与M决定在KFC约会,但是有多家KFC,所以两人想选一家花费时间最少的KFC,给定Y与M的初始位置,KFC的位置,可以上下左右走,一次花费11分钟。

题解:两人bfs,但是只需记录一次kfc的位置,然后记录两人分别到每家kfc花费的最少时间,两人到同一家店花费时间相加最少即符合要求。


代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <algorithm>using namespace std;const int maxn = 1010; // 不要问为什么开1010--试一试你就知道const int inf = 0x3f3f3f3f;char Map[maxn][maxn]; // 图bool vis[maxn][maxn]; // 标记是否走过int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 四个行走方向int timeY[maxn][maxn]; // 记录Y到某位置所用时间int timeM[maxn][maxn]; // 记录M到某位置所用时间int n, m, k; // k为 kfc 个数struct node // 位置与步数{    int x, y;    int step;}Y, M, K[maxn]; // K 记录kfc位置bool check(int x, int y) // 检查是否越界 是否走过 是否撞墙{    if (x < 1 || x > n || y < 1 || y > m)        return true;    if (vis[x][y] || Map[x][y] == '#')        return true;    return false;}bfsY(){    memset(vis, 0, sizeof(vis));    queue <node> Q; // 标准化操作    node now, next;    now.x = Y.x, now.y = Y.y, now.step = 0;    vis[Y.x][Y.y] = true;    Q.push(now);    while (!Q.empty())    {        now = Q.front();        Q.pop();        if (Map[now.x][now.y] == '@') // 符合要求 -- 记录 -- 继续寻找        {            timeY[now.x][now.y] = now.step; // 时间--步数            K[k].x = now.x; // 坐标位置            K[k++].y = now.y;            continue; // 62ms--93ms        }        for (int i = 0; i < 4; ++i) // 标准化操作        {            next = now;            next.x = now.x + dir[i][0];            next.y = now.y + dir[i][1];            if (check(next.x, next.y))                continue;            vis[next.x][next.y] = true;            next.step = now.step + 1;            Q.push(next);        }    }}bfsM() // 同上{    memset(vis, 0, sizeof(vis));    queue <node> Q;    node now, next;    now.x = M.x, now.y = M.y, now.step = 0;    vis[M.x][M.y] = true;    Q.push(now);    while (!Q.empty())    {        now = Q.front();        Q.pop();        if (Map[now.x][now.y] == '@') // 只需记录时间 -- 位置已标记            timeM[now.x][now.y] = now.step;        for (int i = 0; i < 4; ++i)        {            next = now;            next.x = now.x + dir[i][0];            next.y = now.y + dir[i][1];            if (check(next.x, next.y))                continue;            vis[next.x][next.y] = true;            next.step = now.step + 1;            Q.push(next);        }    }}int main(){    //freopen("in.txt", "r", stdin);    while (scanf("%d%d", &n, &m) == 2)    {        k = 0;        for (int i = 1; i <= n; ++i)            for (int j = 1; j <= m; ++j)            {                cin >> Map[i][j];                if (Map[i][j] == 'Y')                    Y.x = i, Y.y = j;                else if (Map[i][j] == 'M')                    M.x = i, M.y = j;            }        bfsY();        bfsM();        int ans = inf, temp;        for (int i = 0; i < k; ++i) // 时间更新--得到花费最少时间        {            //printf("%d %d\n", K[i].x, K[i].y);            temp = (timeY[K[i].x][K[i].y] + timeM[K[i].x][K[i].y]) * 11;            ans = min(ans, temp);        }        printf("%d\n", ans);    }    return 0;}

  • The end.
  • 2017年8月9日14:57:02

原创粉丝点击