1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | // 16분 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <vector> #include <algorithm> using namespace std; int n; int map[26][26]; bool check[26][26]; int dx[] = { 1,-1,0,0 }; int dy[] = { 0,0,1,-1 }; int dfs(int row, int col) { check[row][col] = true; int temp = 1; for (int k = 0; k < 4; k++) { int ny = row + dy[k]; int nx = col + dx[k]; if (0 <= nx && nx < n && 0 <= ny && ny < n) { if (map[ny][nx] == 1 && !check[ny][nx]) { temp += dfs(ny, nx); } } } return temp; } void solve() { vector<int> a; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (map[i][j] == 1 && !check[i][j]) { a.push_back(dfs(i, j)); } } } sort(a.begin(), a.end()); printf("%d\n", a.size()); for (int i = 0; i < a.size(); i++) { printf("%d\n", a[i]); } } int main() { cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%1d", &map[i][j]); } } solve(); return 0; } | cs |
백준 2468 안전 영역 (0) | 2018.05.17 |
---|---|
백준 11724 연결 요소의 개수 (0) | 2018.05.17 |
백준 1012 유기농 배추 (0) | 2018.05.17 |
백준 2583 영역 구하기 (0) | 2018.05.17 |
백준 1260 DFS와 BFS (0) | 2018.05.16 |
댓글,
jayharvey
머신러닝/딥러닝 관련 글을 포스팅할 예정입니다 :)