【HDU 2612】Find a way(BFS)

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)相约到KFC吃东西,‘@’表示是KFC所在的位置,题目要求你找到一个KFC使得两个人走路所花的时间最短(每走一步需要11分钟),注意KFC也是可以作为路的,不是说遇到KFC不能通过。

思路

思路很简单,分别对Y和M跑一遍bfs,用两个数组分别记录Y和M在各个点的步数,最后选择所有KFC所在点,将两个数组对应这点的步数相加找到一个最小的即可。
这边有一个特别需要注意的地方,那就是初始化这两个数组时,一定要初始化为比较大的一个数,不能初始化为0,为什么呢?可以看下面这个样例:

4 4
Y.#@
....
##..
@#.M

(4,1)这个点是没法到达的,但是如果你初始化为0,那么这一点的步数是0,那你在选最小值时,这点也是会被选上的。
详细实现看代码:

代码

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

using namespace std;
const int maxn=200+5;
const int INF=0x3f3f3f3f;//一个较大的数

int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
char map[maxn][maxn];
int vis[maxn][maxn],n,m,y_step[maxn][maxn],m_step[maxn][maxn];
//y_step[maxn][maxn]是y在每一点的步数,同理m_step[maxn][maxn]

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

void bfs(int x,int y,char id)//id用于标记是y还是m。
{
    //初始化一定不能漏,并且步数一定要初始化为一个比较大的数。
    memset(vis,0,sizeof(vis));
    if(id=='y') memset(y_step,INF,sizeof(y_step));
    else memset(m_step,INF,sizeof(m_step));
    queue<proc> q;
    proc vw,vn;
    vw.x=x;
    vw.y=y;
    vw.step=0;
    vis[x][y]=1;
    q.push(vw);
    while(!q.empty())
    {
        vw=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            vn.x=vw.x+dir[i][0];
            vn.y=vw.y+dir[i][1];
            if(!vis[vn.x][vn.y]&&map[vn.x][vn.y]!='#'&&vn.x>=0&&vn.x<n&&vn.y>=0&&vn.y<m)
            {
                vis[vn.x][vn.y]=1;
                vn.step=vw.step+1;
                if(id=='y') y_step[vn.x][vn.y]=vn.step;
                else m_step[vn.x][vn.y]=vn.step;
                q.push(vn);
            }
        }
    }
}

int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        vector<proc> v;
        v.clear();
        proc m_y,m_m;
        for(int i=0;i<n;i++)
        {
            cin>>map[i];
            for(int j=0;j<m;j++)
            {
                if(map[i][j]=='Y') 
                {
                    m_y.x=i;
                    m_y.y=j;
                }
                if(map[i][j]=='M')
                {
                    m_m.x=i;
                    m_m.y=j;
                }
                if(map[i][j]=='@')
                {
                    v.push_back((proc){i,j});
                }
            }
        }
        bfs(m_y.x,m_y.y,'y');
        bfs(m_m.x,m_m.y,'m');
        int ans=INF;
        for(int i=0;i<v.size();i++)
        {
            //cout<<"v[i].x:"<<v[i].x<<" v[i].y:"<<v[i].y<<" y_step:"<<y_step[v[i].x][v[i].y]<<" m_step:"<<m_step[v[i].x][v[i].y]<<endl;
            if((y_step[v[i].x][v[i].y]+m_step[v[i].x][v[i].y])<ans)
            {
                ans=y_step[v[i].x][v[i].y]+m_step[v[i].x][v[i].y];
            }
        }
        printf("%d\n",ans*11);
    }
    return 0;
} 

猜你喜欢

转载自blog.csdn.net/iceiceicpc/article/details/52101243