AcWing 1098. 城堡问题(洪水填充)

2021-03-23 11:57:54 浏览数 (1)

思路:洪水填充 这道题目的墙一看就知道是二进制拆分,注意洪水填充一进队就要标记~

代码语言:javascript复制
#include<bits/stdc  .h>
using namespace std;
typedef pair<int,int> pii;
#define x first
#define y second
const int N=55;
int m,n,g[N][N],area,cnt,st[N][N];
int dx[]={0,-1,0,1},dy[]={-1,0,1,0};
int bfs(int sx,int sy){
    queue<pii>q;
    q.push({sx,sy});
    int tep=0;
    while(q.size()){
        auto p=q.front();
        //cout<<p.x<<" "<<p.y<<endl;
        st[p.x][p.y]=1;
        tep  ;
        q.pop();
        for(int i=0;i<4;i  ){
            if(g[p.x][p.y]>>i&1)continue;
            int nx=p.x dx[i],ny=p.y dy[i];
            if(nx<1||ny<1||nx>m||ny>n||st[nx][ny])continue;

            //cout<<"nx ny"<<" "<<nx<<" "<<ny<<endl;
            q.push({nx,ny});
            st[nx][ny]=1;
        }
    }
    //cout<<tep<<endl;
    return tep;
}
int main(){
    cin>>m>>n;
    for(int i=1;i<=m;i  ){
        for(int j=1;j<=n;j  )cin>>g[i][j];
    }
    for(int i=1;i<=m;i  ){
        for(int j=1;j<=n;j  ){
            if(!st[i][j]){
                cnt  ;
                area=max(area,bfs(i,j));
            }
        }
    }
    cout<<cnt<<endl<<area<<endl;
}

0 人点赞