#include <stdio.h> #include <iostream> #include <vector> #include <algorithm> using namespace std; int N; int map[30][30]; int visited[30][30]; int houseNum; int Size; vector<int> complex; int dx[4] = {-1, 1, 0, 0}; // 상하좌우 int dy[4] = {0, 0, -1, 1}; // 상하좌우 int safe(int x, int y) { if(x >= 0 && y >= 0 && x < N && y < N) { return 1; } else { return 0; } } void DFS(int x, int y, int color) { for(int i = 0; i < 4; i++) { int xpos = x+dx[i]; int ypos = y+dy[i]; if(safe(xpos, ypos) == 1 && map[xpos][ypos] == 1 && visited[xpos][ypos] == 0) { visited[xpos][ypos] = color; DFS(xpos, ypos, color); Size++; } } } int main(void) { // freopen("B2667_input.txt", "r", stdin); cin >> N; for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { scanf("%1d", &map[i][j]); } } for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { if(map[i][j] == 1 && visited[i][j] == 0) { Size = 1; houseNum++; visited[i][j] = houseNum; DFS(i, j, houseNum); complex.push_back(Size); } } } cout << houseNum << endl; sort(complex.begin(), complex.end()); for(int i = 0; i < complex.size(); i++) { cout << complex[i] << endl; } return 0; } | cs |
'Baekjoon > DFS' 카테고리의 다른 글
[백준 1068] 트리 (DFS, Tree) (C/C++) (★) (0) | 2020.02.16 |
---|---|
[백준 9466] 텀 프로젝트 (DFS) (C/C++) (★★★) (0) | 2020.01.26 |
[백준 10451] 순열 사이클 (DFS) (C/C++) (0) | 2020.01.26 |
[백준 13023] ABCDE (DFS) (C/C++) (0) | 2019.12.08 |
[백준 14500] 테트로미노 (DFS) (C/C++) (★) (0) | 2019.11.19 |