萌新刚学 OI,简单广搜20pts求调
查看原帖
萌新刚学 OI,简单广搜20pts求调
945742
hexuchen楼主2024/8/28 18:36

rt,记录,代码:

#include <bits/stdc++.h>
using namespace std;
struct dot{
	int a,b;
};
dot s[10001][10001];
int n,ans=0,anss[10001],a=0;
int dh[5]={0,-1,0,1,0};
int dl[5]={0,0,1,0,-1};
void bfs(int x,int y){
	int maxn=0;
	queue<int> h;
	queue<int> l;
	h.push(x);
	l.push(y);
	s[x][y].a=0;
	s[x][y].b=1;
	do{
		int xx=h.front();
		int yy=l.front();
		h.pop();
		l.pop();
		for(int i=1;i<=4;i++){
			int hh=xx+dh[i],ll=yy+dl[i];
			if(hh>=1 && hh<=n && ll>=1 && ll<=n && s[hh][ll].a){
				h.push(hh);
				l.push(ll);
				s[hh][ll].a=0;
				s[hh][ll].b=s[xx][yy].b+1;
				maxn=max(maxn,s[hh][ll].b);
			}
		}
	}while(!h.empty());
	anss[++ans]=maxn;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			char c;
			cin>>c;
			if(c=='0'){
				s[i][j].a=0;
			}
			else{
				s[i][j].a=1;
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(s[i][j].a){
				bfs(i,j);
				/*cout<<endl;
				for(int i=1;i<=n;i++){
				for(int j=1;j<=n;j++){
				cout<<s[i][j];
				}
				cout<<endl;
				}
				cout<<endl;*/
			}
		}
	}
	cout<<ans<<'\n';
	sort(anss+1,anss+1+ans);
	for(int i=1;i<=ans;i++){
		cout<<anss[i]<<'\n';
	}
	return 0;
}
2024/8/28 18:36
加载中...