1 solutions

  • 0
    @ 2022-2-20 13:47:28
    #include<bits/stdc++.h>
    using namespace std;
    int n,ans;
    const int N=1e3+5;
    char f[N][N];
    int dx[8]={0,0,1,1,1,-1,-1,-1};
    int dy[8]={1,-1,1,0,-1,1,-1,0};
    void dfs(int x,int y){
    	f[x][y]='0';
    	for(int i=0;i<8;i++){
    		int nx=x+dx[i];
    		int ny=y+dy[i];
    		if(nx>0&&nx<=n&&ny>=0&&ny<n&&f[nx][ny]=='1'){
    			dfs(nx,ny);
    		}
    	}
    	return ;
    }
    int main(){
    	ans=0;
    	cin>>n;
    	for(int i=1;i<=n;i++){
    		cin>>f[i];
    	}
    	for(int i=1;i<=n;i++){
    		for(int j=0;j<n;j++){
    			if(f[i][j]=='1'){
    				dfs(i,j);
    				ans++;
    			}
    		}
    	}
    	cout<<ans;
    	return 0;
    }
    • 1

    Information

    ID
    1404
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    9
    Tags
    # Submissions
    12
    Accepted
    7
    Uploaded By