比赛 板子大赛 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 黑白图像 最终得分 100
用户昵称 秋_Water 运行时间 0.377 s
代码语言 C++ 内存使用 4.20 MiB
提交时间 2025-01-22 10:36:16
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
int dx[8]={1,1,1,-1,-1,-1,0,0};
int dy[8]={0,-1,1,0,-1,1,1,-1};
int w[1000][1000],n,ans;
struct ndoe{
    int x,y;
};
void bfs(int x,int y){
    queue<ndoe>q;
    q.push({x,y});
    while(!q.empty()){
        ndoe now=q.front();
        q.pop();        
        int ii=now.x;
        int jj=now.y;
        for(int i=0;i<8;i++){
            int xx=ii+dx[i];
            int yy=jj+dy[i];
            if(xx>0&&yy>0&&xx<=n&&yy<=n&&w[xx][yy]==1){
                w[xx][yy]=0;
                q.push({xx,yy});
            }
        }   
    }
}

int main(){
    freopen("common.in","r",stdin);
    freopen("common.out","w",stdout);   
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            scanf("%1d",&w[i][j]);
        }
    }   
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(w[i][j]==1){
                bfs(i,j);
                ans++;
            }
        }
    }    
    cout<<ans;
    
    return 0;
}