HDU 2653 BFS+優先隊列

來源:互聯網
上載者:User

真是頭大。老是犯些超級低級的錯誤;

數組我存的時候下標寫的是 1 開始,

我用check函數判斷的時候寫的確實 a.x<0||b.x<0

一個錯誤找一天。

唉。。。說多了都是淚;

發現錯了就重新拍代碼,拍了還是犯這樣的錯;  絕對是太粗心了;

///////////////////////////////////

********************

思路:

對每個點 進行走路和飛行的判斷和遍曆;

visit 三位元組 存放當前點 當前magic是否遍曆過。

********************

/////////////////////////////////

/*  *Author   ID:fuqiang11   *Problem  ID:HDU 2653  *Submit Time:2013/7/26  *Algorithm  :BFS + priority_queue*/  #include <iostream>#include <cstdio>#include <string>#include <queue>#include <cstring>using namespace std;#define maxn 82int N,M,T,P;struct point{    int x;    int y;    int t;    int p;    friend bool operator < (point a, point b)    {        return a.t > b.t;    }}st,ed;char maze[maxn][maxn];int visit[maxn][maxn][maxn];int xx[] = {0,0,1,-1};int yy[] = {1,-1,0,0};bool check(point a){    if(a.x<1||a.y<1||a.x>N||a.y>M||maze[a.x][a.y]=='#'||visit[a.x][a.y][a.p])        return false;    return true;}priority_queue <point>  q;void BFS(){    while(!q.empty()) //release queue        q.pop();    q.push(st);    point a,b;    while(!q.empty())    {        a = q.top();        q.pop();        if(a.t > T)            break;        if(a.x==ed.x && a.y==ed.y)        {            printf("Yes, Yifenfei will kill Lemon at %d sec.\n",a.t);            return ;        }        for(int i = 0; i < 4; i++)        {            b.x = a.x + xx[i];            b.y = a.y + yy[i];            b.t = a.t;            b.p = a.p;            if(check(b))            {                if(maze[a.x][a.y]!='@' && maze[b.x][b.y]!='@') //能走能飛的情況 選擇走                {                    visit[b.x][b.y][b.p] = 1;                    b.t += 2;                    q.push(b);                    b.t -= 2;                }                if(!visit[b.x][b.y][b.p-1] && b.p>0)//選擇飛                {                    b.p -= 1;                    b.t += 1;                    visit[b.x][b.y][b.p] = 1;                    q.push(b);                }            }        }    }//end while    printf("Poor Yifenfei, he has to wait another ten thousand years.\n");    return ;}int main(){    int Case = 1;    while(scanf("%d%d%d%d",&N,&M,&T,&P)!=EOF)    {        int i,j;        for (i=1;i<=N;i++) scanf("%s",maze[i]+1);        for (i=1;i<=N;i++)        {            for (j=1;j<=M;j++)            {                if (maze[i][j]=='Y') {st.x=i; st.y=j;}                if (maze[i][j]=='L') {ed.x=i; ed.y=j;}            }        }        printf("Case %d:\n",Case++);        memset(visit,0,sizeof(visit));        st.p = P;        st.t = 0;        visit[st.x][st.y][st.p] = 1;        BFS();    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.