-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-3.cpp
More file actions
66 lines (56 loc) · 1 KB
/
5-3.cpp
File metadata and controls
66 lines (56 loc) · 1 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
/**
* @file 5-3.cpp
* @brief 음료수 얼려먹기
* @author Sam Kim (samkim2626@gmail.com)
*
* 1. 왜 DFS로 풀어야겠다고 생각?
* 2.
*/
#include <iostream>
#include <vector>
using namespace std;
int n, m;
int graph[1001][1001];
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 - 1, y);
dfs(x, y + 1);
dfs(x, y - 1);
return true;
}
return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
scanf("%1d", &graph[i][j]);
}
}
int rst = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (dfs(i, j))
{
rst++;
}
}
}
cout << rst << '\n';
return 0;
}