当前位置:首页 > Windows程序 > 正文

AcWing池塘计数

这个题让我们求连通块得数数量,我考虑用flood fill算法。

也就是枚举这个地图每一个点,假如符合要求就bfs与这个点联通的点,并打上标记。结束后接着枚举没有被标记并且符号要求的点。。。

1.==和=千万别写错

2.队列不要忘记head++;

3.tail++一遍就可以了

代码

#include<bits/stdc++.h> #define maxn 1010 using namespace std; char mp[maxn][maxn]; int n,m; struct node{ int x,y; }q[maxn*maxn]; int ans=0; bool book[maxn][maxn]; void bfs(int sx,int sy){ int head=1,tail=0; q[++tail].x=sx; q[tail].y=sy; book[sx][sy]=true; while(head<=tail){ for(int i=q[head].x-1;i<=q[head].x+1;i++){ for(int j=q[head].y-1;j<=q[head].y+1;j++){ if(i==q[head].x&&j==q[head].y) continue; if(i<1||i>n||j<1||j>m) continue; if(mp[i][j]==.||book[i][j]==true) continue; if(book[i][j]==false&&mp[i][j]==W){ q[++tail].x=i; q[tail].y=j; book[i][j]=true; } } } head++; } } int cnt=0; int main(){ cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>mp[i][j]; } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(mp[i][j]==W&&book[i][j]==false){ cnt++; bfs(i,j); } } } cout<<cnt; return 0; }

AcWing池塘计数

标签:

原文地址:https://www.cnblogs.com/china-mjr/p/11802529.html

温馨提示: 本文由杰米博客推荐,转载请保留链接: https://www.jmwww.net/file/10146.html