HDU 2612 BFS*2

來源:互聯網
上載者:User

*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 0x3f3f3f3fusing 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;    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.