본문 바로가기

카테고리 없음

백준 2667

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#define size 26

using namespace std;


int arr[size][size];
bool check[size][size] = { false, };
vector<int>cnt_d;
int posX[4] = { 0,0,-1,1 };
int posY[4] = { 1,-1 ,0,0, };

string temp;
int n;
int cal = 0;
int cal_d=0;
int nx, ny;
int x, y;

void dfs(int x, int y) {

	cal_d++;
	for (int i = 0; i < 4; i++) {

		nx = x + posX[i];
		ny = y + posY[i];

		if (nx >= 0 && ny >= 0 && nx < n && ny < n) {
			if (arr[nx][ny] == 1 && check[nx][ny] == false) {
				check[nx][ny] = true;
				dfs(nx, ny);
			}
		}
	}

}

int main() {


	    cin.tie(0);
		ios_base::sync_with_stdio(false);
		
		cin >> n;
		

		for (int i = 0; i < n; i++) {
			
			cin >> temp;

			for (int j = 0; j < n; j++) {
			
				
				arr[i][j] = temp[j] - '0';
			}
		
		}

		for (int i = 0; i < n; i++) {

			for (int j = 0; j < n; j++) {

				if (arr[i][j] == 1 && check[i][j] == false) {

					cal = cal + 1;
					check[i][j] = true;
					dfs(i, j);
					cnt_d.push_back(cal_d);
					cal_d = 0;
				}
			}
		}

		cout << cal;
		cout << endl;

		sort(cnt_d.begin(), cnt_d.end());

		for (int i = 0; i < cal; i++) {
			cout << cnt_d[i];
			cout << endl;
		}

	
		
	
		

}