-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ_1012_유기농배추.cpp
More file actions
58 lines (57 loc) · 1.38 KB
/
BJ_1012_유기농배추.cpp
File metadata and controls
58 lines (57 loc) · 1.38 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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main() {
int i, j, k, x, y;
int test, row, column, n;
int count = 0;
cin >> test;
queue<pair<int, int>> worm;
vector<int> needed;
for (i = 0; i < test; i++) {
cin >> row >> column >> n;
int farm[50][50] = { 0 };
bool visited[50][50] = { false };
// 1. ÀÔ·Â
for (j = 0; j < n; j++) {
cin >> x >> y;
farm[x][y] = 1;
}
// 2. ã±â
for (j = 0; j < row; j++) {
for (k = 0; k < column; k++) {
if (farm[j][k] == 1 && !visited[j][k]) {
visited[j][k] = true;
count++;
worm.push(make_pair(j, k));
}
while (!worm.empty()) {
x = worm.front().first;
y = worm.front().second;
worm.pop();
if (x - 1 >= 0 && farm[x - 1][y] == 1 && !visited[x - 1][y]) {
visited[x-1][y] = true;
worm.push(make_pair(x-1, y));
}
if (x + 1 < row && farm[x+1][y] == 1 && !visited[x+1][y]) {
visited[x+1][y] = true;
worm.push(make_pair(x+1, y));
}
if (y - 1 >= 0 && farm[x][y-1] == 1 && !visited[x][y-1]) {
visited[x][y-1] = true;
worm.push(make_pair(x, y-1));
}
if (y+1 < column && farm[x][y+1] == 1 && !visited[x][y+1]) {
visited[x][y+1] = true;
worm.push(make_pair(x, y+1));
}
} }
}
needed.push_back(count);
count = 0;
}
// 3. Ãâ·Â
for (i = 0; i < needed.size(); i++)
cout << needed[i] << endl;
}