안선생의 개발 블로그
C++ 2667 단지번호붙이기 본문
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
|
#include <iostream>
#include <vector>
#include<queue>
#include<algorithm>
using namespace std;
vector<int> v;
int g[30][30] = {};
int aaa = 0;
int n;
bool dfs(int x,int y)
{
if (x <= -1 || x >= n || y <= -1 || y >= n)
return false;
if (g[x][y])
{
aaa++;
g[x][y] = 0;
dfs(x - 1, y);
dfs(x + 1, y);
dfs(x , y - 1);
dfs(x , y + 1);
return true;
}
return false;
}
int main(void)
{
int z = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
string s;
cin >> s;
for (int j = 0; j < n; j++)
{
g[i][j]=(s[j] - '0');
}
}
int result = 0;
for (int i = 0; i <= n; i++)
{
for (int j = 0; j < n; j++)
{
if (dfs(i, j))
{
result += 1;
v.push_back(aaa);
aaa = 0;
}
}
}
cout << result <<"\n";
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << "\n";
}
}
|
cs |
https://www.acmicpc.net/problem/2667
2667번: 단지번호붙이기
<그림 1>과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여
www.acmicpc.net
'백준' 카테고리의 다른 글
C++ 24479 알고리즘 수업 - 깊이 우선 탐색 1 (0) | 2023.02.16 |
---|---|
C++ 11724 연결 요소의 개수 (0) | 2023.02.16 |
C++ 2606 바이러스 (0) | 2023.02.14 |
C++ 7568 덩치 (0) | 2023.02.12 |
C++ 1927 최소힙 (0) | 2023.02.12 |