반응형
https://www.acmicpc.net/problem/1012
1012번: 유기농 배추
차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에 효과적인 배추흰지렁이를 구입하기로 결심한다. 이 지렁이는 배추근처에 서식하며 해충을 잡아 먹음으로써 배추를 보호한다. 특히, 어떤 배추에 배추흰지렁이가 한 마리라도 살고 있으면 이 지렁이는 인접한 다른 배추로 이동할 수 있어, 그 배추들 역시 해충으로부터 보호받을 수 있다. (
www.acmicpc.net
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
|
반응형