반응형
https://www.acmicpc.net/problem/1012
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
|
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
int T;
int M, N, K;
void DFS(vector<vector<int>>& v, vector<vector<bool>>& check, int y, int x) {
if (y < 0 || y >= N || x < 0 || x >= M) return;
if (v[y][x] == 0) return;
if (check[y][x] == true) return;
check[y][x] = true;
DFS(v, check, y - 1, x);
DFS(v, check, y + 1, x);
DFS(v, check, y, x - 1);
DFS(v, check, y, x + 1);
}
int DFS_start(vector<vector<int>>& v, vector<vector<bool>>& check) {
int cnt = 0;
for (int i = 0;i < v.size();i++) {
for (int j = 0;j < v[i].size();j++) {
if (check[i][j] == false && v[i][j] == 1) {
++cnt;
DFS(v, check, i, j);
}
}
}
return cnt;
}
int main(void) {
cin >> T;
while (T--) {
cin >> M >> N >> K;
vector <vector<int>> v(N, vector<int>(M, 0));
vector <vector<bool>> check(N, vector<bool>(M, false));
int y, x;
while (K--) {
cin >> x >> y;
v[y][x] = 1;
}
cout << DFS_start(v, check) << endl;
}
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
반응형