-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj_1012.cpp
More file actions
67 lines (56 loc) · 1.21 KB
/
boj_1012.cpp
File metadata and controls
67 lines (56 loc) · 1.21 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
//<1012>번 : <유기농 배추>
#include <bits/stdc++.h>
using namespace std;
int t, n, m, k, cnt;
bool isCount = false;
bool field[50][50] = {};
bool visited[50][50] = {};
void dfs(int y, int x)
{
if(!field[y][x] || visited[y][x]) return;
isCount = true;
visited[y][x] = true;
if(x > 0) dfs(y, x-1);
if(x < m-1) dfs(y, x+1);
if(y > 0) dfs(y-1, x);
if(y < n-1) dfs(y+1, x);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> t;
for(int p = 0; p < t; p++)
{
cin >> m >> n >> k;
int x, y;
cnt = 0;
for(int i = 0; i < k; i++)
{
cin >> x >> y;
field[y][x] = true;
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
dfs(i,j);
if(isCount)
{
cnt++;
isCount = false;
}
}
}
cout << cnt << '\n';
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
field[i][j] = false;
visited[i][j] = false;
}
}
}
return 0;
}