hdu 1312 poj 1979 Red and Black BFS

來源:互聯網
上載者:User

題目  http://poj.org/problem?id=1979  

  這是一個簡單的BFS   剛開始調試老錯了  對不上答案  很糾結  最後發現原來我的w 和 h搞反了哦   含糾結。。。。

   做這個題告訴我   下次題目一定要 小心啊   不要犯這個低級錯誤。。呵呵

下面是AC代碼

#include<iostream>#include<queue>#include<cstdio>using namespace std;struct node{int x,y;};char a[21][21];int w,h,visit[21][21],sx,sy;  ///sx sy @ 起始位置  visit標記int dre[4][2]={0,1,1,0,0,-1,-1,0}; ///上右下左void Bfs(){    queue<node> Q;    node p,s;    p.x=sx;    p.y=sy;    Q.push(p);    int sum=1;    visit[sx][sy]=1; ///標記    while(!Q.empty())    {        s=Q.front();        Q.pop();        for(int i=0;i<4;i++)        {            p=s;            p.x+=dre[i][0];   ///上右下左            p.y+=dre[i][1];            if(a[p.x][p.y]=='#') continue;  ///如果是#就退出來            if(p.x>0&&p.x<=h&&p.y>0&&p.y<=w)  ///要在這個長方形下面。這樣h 和w位置            {                if(a[p.x][p.y]=='.'&&visit[p.x][p.y]==0)  ///標記一下                {                    visit[p.x][p.y]=1;   ///標記                    sum++;   ///計算一共有多少 . 和                    Q.push(p);                }            }        }    }    cout<<sum<<endl;}int main(){    int i,j;    while(scanf("%d%d",&w,&h)!=EOF,w+h)    {        for(i=1;i<=h;i++)  ///注意h和w位置         for(j=1;j<=w;j++)          {              cin>>a[i][j];              if(a[i][j]=='@'){sx=i;sy=j;}  ///記錄起始位置。              visit[i][j]=0;          }        Bfs();    }    return 0;}

 

聯繫我們

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