Total passenger garlic Informatics popular group before the analog # 1 C colors

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Glynn/article/details/102760825

Total passenger garlic Informatics popular group before the analog # 1

C Color

No nonsense, look at the code.

#include<queue>
#include<set>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

struct node{int x,y;};
const int N=1001;
int n,m,ans=0;
int d[N][N]={0};
bool v[N][N]={0};
set<int> si;

int main(){
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){ scanf("%d",&d[i][j]); }
        }
        for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                        if(d[i][j]==0 || v[i][j]) continue;
                        si.clear();
                        queue<node> q;
                        q.push((node){i,j});
                        while(!q.empty()){
                                int x=q.front().x;
                                int y=q.front().y;
                                q.pop();
                                for(int k=0;k<4;k++){
                                        int xk=x,yk=y;
                                        if(k==0)xk++; else if(k==1)yk++; else if(k==2)xk--; else yk--;
                                        if(xk<0 || xk>=n || yk<0 || yk>=m || d[xk][yk]==0 || v[xk][yk])continue;
                                        v[xk][yk]=true;
                                        si.insert(d[xk][yk]);
                                        q.push((node){xk,yk});
                                }
                        }
                        ans=max(ans,(int)si.size());
                }
        }
        printf("%d\n",ans);
        return 0;
}

 

Guess you like

Origin blog.csdn.net/Glynn/article/details/102760825
Recommended