forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10.cpp
More file actions
48 lines (44 loc) ยท 1.28 KB
/
10.cpp
File metadata and controls
48 lines (44 loc) ยท 1.28 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
#include <bits/stdc++.h>
using namespace std;
int n, m;
int graph[1000][1000];
// DFS๋ก ํน์ ๋
ธ๋๋ฅผ ๋ฐฉ๋ฌธํ๊ณ ์ฐ๊ฒฐ๋ ๋ชจ๋ ๋
ธ๋๋ค๋ ๋ฐฉ๋ฌธ
bool dfs(int x, int y) {
// ์ฃผ์ด์ง ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ ๊ฒฝ์ฐ์๋ ์ฆ์ ์ข
๋ฃ
if (x <= -1 || x >=n || y <= -1 || y >= m) {
return false;
}
// ํ์ฌ ๋
ธ๋๋ฅผ ์์ง ๋ฐฉ๋ฌธํ์ง ์์๋ค๋ฉด
if (graph[x][y] == 0) {
// ํด๋น ๋
ธ๋ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
graph[x][y] = 1;
// ์, ํ, ์ข, ์ฐ์ ์์น๋ค๋ ๋ชจ๋ ์ฌ๊ท์ ์ผ๋ก ํธ์ถ
dfs(x - 1, y);
dfs(x, y - 1);
dfs(x + 1, y);
dfs(x, y + 1);
return true;
}
return false;
}
int main() {
// N, M์ ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถํ์ฌ ์
๋ ฅ ๋ฐ๊ธฐ
cin >> n >> m;
// 2์ฐจ์ ๋ฆฌ์คํธ์ ๋งต ์ ๋ณด ์
๋ ฅ ๋ฐ๊ธฐ
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%1d", &graph[i][j]);
}
}
// ๋ชจ๋ ๋
ธ๋(์์น)์ ๋ํ์ฌ ์๋ฃ์ ์ฑ์ฐ๊ธฐ
int result = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// ํ์ฌ ์์น์์ DFS ์ํ
if (dfs(i, j)) {
result += 1;
}
}
}
cout << result << '\n'; // ์ ๋ต ์ถ๋ ฅ
}