HDU 2612 BFS*2

*2 就是2次了;呵呵

题目很坑爹,描述模糊不清.

注意几点:

*1.Y点M点相当于墙,不能穿过(虽然不符合常理);

*2.@可以穿过

另外,题目其实说是求距离和最短还差不多,时间最短不合理了,每人都需要3分钟到那儿,一共需要多少分钟,还是3分钟.但是他叠加了;

这到是题外话;

代码:

/*
 *HDU 2612
 *fuqiang11
 *BFS
 *2013/7/31
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define maxn 200+3
#define INF 0x3f3f3f3f
using namespace std;

int n,m;
char maze[maxn][maxn];
bool vis[maxn][maxn];
int dis[maxn][maxn];
int xx[] = {0,0,1,-1};
int yy[] = {1,-1,0,0};
struct point
{
    int x;
    int y;
    int d;
};
point YF,MER,KFC[maxn*maxn];  //YuFeiFei 和 Merceki 位置 以及 kfc 店位置
int Nkfc;//统计KFC店数量

bool check(int x, int y)
{
    if(x<1||y<1||x>n||y>m||maze[x][y]=='#'||vis[x][y])
        return false;
    return true;
}

queue <point> q;
void BFS(point st)
{
    memset(vis,false,sizeof(vis));
    memset(dis,0x3f,sizeof(dis));
    while(!q.empty())
        q.pop();
    st.d = 0;
    q.push(st);
    vis[st.x][st.y] = true;
    dis[st.x][st.y] = 0;
    point a,b;
    while(!q.empty())
    {
        a = q.front();
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            b.x = a.x + xx[i];
            b.y = a.y + yy[i];
            b.d = a.d + 1;
            if(check(b.x, b.y))
            {
                q.push(b);
                vis[b.x][b.y] = true;
                dis[b.x][b.y] = b.d;
            }
        }
    }
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in","r",stdin);
#endif
    while(cin>>n>>m)
    {
        Nkfc = 0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                cin>>maze[i][j];
                if(maze[i][j] == '@')
                {
                    KFC[Nkfc].x = i;
                    KFC[Nkfc].y = j;
                    Nkfc++;
                    maze[i][j] = '.';
                }
                else if(maze[i][j] == 'Y')
                {
                    YF.x = i;
                    YF.y = j;
                    maze[i][j] = '#';
                }
                else if(maze[i][j] == 'M')
                {
                    MER.x = i;
                    MER.y = j;
                    maze[i][j] = '#';
                }
            }
        }
        BFS(YF);
        for(int i = 0; i < Nkfc; i++)
        {
            KFC[i].d = dis[KFC[i].x][KFC[i].y];
//            cout<<KFC[i].d<<" ";
        }
        BFS(MER);
        int MIN_D = INF;
        for(int i = 0; i < Nkfc; i++)
        {
            if(KFC[i].d + dis[KFC[i].x][KFC[i].y] < MIN_D)
            {
                MIN_D = KFC[i].d + dis[KFC[i].x][KFC[i].y];
            }
        }
        cout<<MIN_D*11<<endl;
    }
}


你可能感兴趣的:(HDU 2612 BFS*2)