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;
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章