-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2667.cpp
More file actions
103 lines (88 loc) · 1.96 KB
/
2667.cpp
File metadata and controls
103 lines (88 loc) · 1.96 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
작성자 : 박진현
문제 : 단지번호 붙이기, 백준 문제
*/
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
using namespace std;
int N;
vector<int> answer;
int ** map;
int ** answer_map;
int ** visit;
// NORTH : 0, SOUTH : 1, EAST: 2, WEST: 3
int r_dir[4] = { 1, -1, 0, 0 };
int c_dir[4] = { 0, 0, 1, -1 };
void DFS(int row, int col, int cnt) {
for (int i = 0; i < 4; i++) {
if (row + r_dir[i] >= 0 && row + r_dir[i] < N && col + c_dir[i] >= 0 && col + c_dir[i] < N) {
if (!visit[row + r_dir[i]][col + c_dir[i]] && map[row + r_dir[i]][col + c_dir[i]]) {
visit[row + r_dir[i]][col + c_dir[i]] = 1;
answer_map[row + r_dir[i]][col + c_dir[i]] = cnt;
DFS(row + r_dir[i], col + c_dir[i], cnt);
}
}
}
}
int main(int argc, const char * argv[]) {
int cnt = 1;
scanf("%d", &N);
map = (int **)malloc(sizeof(int *) * N);
answer_map = (int **)malloc(sizeof(int *) * N);
visit = (int **)malloc(sizeof(int *) * N);
for (int i = 0; i < N; i++) {
map[i] = (int *)malloc(sizeof(int) * N);
answer_map[i] = (int *)malloc(sizeof(int) * N);
visit[i] = (int *)malloc(sizeof(int) * N);
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
visit[i][j] = 0;
answer_map[i][j] = 0;
}
}
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] && !visit[i][j]) {
visit[i][j] = 1;
answer_map[i][j] = cnt;
DFS(i, j, cnt++);
}
}
}
for (int i = 1; i <= cnt - 1; i++) {
int count = 0;
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
if (answer_map[j][k] == i)
count++;
}
}
answer.push_back(count);
}
sort(answer.begin(), answer.end());
printf("%d\n", cnt - 1);
if (cnt - 1) {
for (int i = 0; i < (int)answer.size(); i++) {
printf("%d\n", answer[i]);
}
}
return 0;
}
/*
7
0110100
0110101
1110101
0000111
0100000
0111110
0111000
*/