ZOJ - 2165 Red and Black

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   for   

BFS題,只是沒有目標位置,只需記下走過的黑色的塊數就行

 1 #include<stdio.h> 2 #include<string.h> 3 const int maxn=1000,maxm=25; 4 int vis[maxm][maxm],mat[maxm][maxm],dir[5][3]={{1,0},{0,-1},{-1,0},{0,1}}; 5 int w,h; 6 char s[maxm][maxm]; 7 struct node{ 8     int xpos; 9     int ypos;10     int step;11     void init(int x,int y,int z)12     {13         xpos=x;14         ypos=y;15         step=z;16     }17 }que[maxn];18 int bfs(node sour);19 int main()20 {21     while(scanf("%d%d",&w,&h)==2 && w && h){22         node source;23         for(int i=0;i<h;i++)24             scanf("%s",s[i]);25         memset(mat,0,sizeof(mat));26         for(int i=0;i<h;i++)27         for(int j=0;j<w;j++){28             if(s[i][j]==‘#‘)29                 mat[i][j]=1;30             else if(s[i][j]==‘@‘){31                 mat[i][j]=0;32                 source.xpos=i;33                 source.ypos=j;34             }35             else36                 continue;37         }38       int maxsteps=bfs(source);39       printf("%d\n",maxsteps);40     }41     return 0;42 }43 int bfs(node sour)44 {45     int maxsteps=1;46     sour.step=1;47     int head,tail;48     que[head=tail=0]=sour;49     tail++;50     memset(vis,0,sizeof(vis));51     vis[sour.xpos][sour.ypos]=1;52     while(head!=tail){53         node a=que[head];54         for(int i=0;i<4;i++){55             int nx=a.xpos+dir[i][0];56             int ny=a.ypos+dir[i][1];57             if(nx>=h || ny>=w || nx<0 || ny<0)58                 continue;59             if(vis[nx][ny])60                 continue;61             if(mat[nx][ny])62                 continue;63             node c;64             c.init(nx,ny,a.step+1);65             que[tail]=c;66             vis[nx][ny]=1;67             maxsteps++;68             tail=(tail+1)%maxn;69         }70         head=(head+1)%maxn;71     }72     return maxsteps;73 }

聯繫我們

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