-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1012.cpp
More file actions
75 lines (68 loc) · 1.05 KB
/
1012.cpp
File metadata and controls
75 lines (68 loc) · 1.05 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
68
69
70
71
72
73
74
75
// 1012. 유기농 배추
// 2019.05.14
// BFS, DFS
#include<iostream>
using namespace std;
int map[51][51];
bool visit[51][51]; //방문 유무를 나타내는 배열 1:방문, 0:방문안함
//상 하 좌 우
int dx[4] = { 0,0,-1,1 };
int dy[4] = { 1,-1,0,0 };
int n, m, k;
void DFS(int x, int y)
{
for (int i = 0; i < 4; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if (xx < 0 || xx >= n || yy < 0 || yy >= m)
{
continue;
}
if (map[xx][yy] && !visit[xx][yy])
{
visit[xx][yy] = 1;
DFS(xx, yy);
}
}
}
int main()
{
int t;
cin >> t;
while (t > 0)
{
t--;
cin >> n >> m >> k;
//초기화
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
map[i][j] = 0;
visit[i][j] = 0;
}
}
int count = 0;
for (int i = 0; i < k; i++)
{
int a, b;
cin >> a >> b;
map[a][b] = 1;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (!visit[i][j] && map[i][j])
{
count++;
visit[i][j] = 1;
DFS(i, j);
}
}
}
cout << count << endl;
}
return 0;
}