-
Notifications
You must be signed in to change notification settings - Fork 0
/
2667.cpp
62 lines (62 loc) · 1.25 KB
/
2667.cpp
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
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
int N;
int board[25][25] = {0};
int dr[4] = {0, 0, 1, -1};
int dc[4] = {1, -1, 0, 0};
int dfs(int row, int col)
{
board[row][col] = 2;
bool flag = true;
int count = 1;
for (int dir = 0; dir < 4; dir++)
{
int nrow, ncol;
nrow = row + dr[dir];
ncol = col + dc[dir];
if (0 <= nrow && nrow < N && 0 <= ncol && ncol < N && board[nrow][ncol] == 1)
{
flag = false;
count += dfs(nrow, ncol);
}
}
if (flag)
return 1;
return count;
}
int main(void)
{
int i, j;
vector<int> ans;
cin >> N;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
scanf("%1d", &board[i][j]);
}
}
int cc = 0;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
if (board[i][j] == 1)
{
ans.push_back(dfs(i, j));
cc++;
}
}
}
cout << cc << endl;
sort(ans.begin(), ans.end());
for (vector<int>::iterator it = ans.begin(); it != ans.end(); it++)
{
cout << *it << endl;
}
return 0;
}