hdu 2612 Find a way (BFS)

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



Author
yifenfei


Source
奋斗的年代


思路:刚开始的时候我是想从一个点搜到一个KFC,再从这个KFC搜到另一个人的距离,最后判断最短的和,但是超时,因为对于每个KFC,都搜了它的数量的两倍。最后想到用一个三维的数组保存每个人到任意一个KFC的距离,最后一次遍历,找最短段的即可。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <queue>
#include <stdlib.h>
#define INF 0x3f3f3f3f
using namespace std;
int dr[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
int vis[202][202];
char map[202][202];
int dis[202][202][2]; ///0代表Y到各个KFC的距离, 1代表M到各个KFC的距离
int n, m, x1, y1, x2, y2;
struct node
{
    int x, y, step;
}p;

void bfs(int x1, int y1, int k)
{
    memset(vis, 0 ,sizeof(vis));
    queue<node>Q;
    node now, next;
    p.x = x1, p.y = y1, p.step = 0;
    Q.push(p);
    vis[x1][y1] = 1;
    while(!Q.empty())
    {
        now = Q.front();
        Q.pop();
        if(map[now.x][now.y] == '@')
        {
            dis[now.x][now.y][k] = now.step;
            //printf("now.x = %d now.y = %d\n", now.x, now.y);
            //printf("%d %d\n",dis[now.x][now.y][k], k);
        }
        for(int i = 0; i < 4; i++)
        {
            next.x = now.x + dr[i][0];
            next.y = now.y + dr[i][1];
            if(next.x >= 0&&next.x < n&&next.y >= 0&&next.y < m&&map[next.x][next.y]!='#'&&!vis[next.x][next.y])
            {
                vis[next.x][next.y] = 1;
                next.step = now.step + 1;
                Q.push(next);
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n, &m))
    {
        int ans = INF;
        memset(dis, INF, sizeof(dis));
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                cin >> map[i][j];
                if(map[i][j] == 'Y')
                {
                    x1 = i, y1 = j;
                }
                else if(map[i][j] == 'M')
                {
                    x2 = i, y2 = j;
                }
            }
        }
        bfs(x1, y1, 0);
        bfs(x2, y2, 1);
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(map[i][j] == '@')  ///遍历找到最短的
                {
                    //printf("%d  %d\n", dis[i][j][0], dis[i][j][1]);
                    ans = min(ans, dis[i][j][0] + dis[i][j][1]);
                }
            }
        }
        printf("%d\n",ans*11);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值