https://www.acmicpc.net/problem/1012
1012번: 유기농 배추
차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에
www.acmicpc.net
이 문제도 배열 행과 열을 꼬아놨다. 체크 잘하자.
반복문을 사용하므로
반목문 마지막에 memset으로 초기화를 시켜준다.
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
76
77
78
79
80
81
82
|
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0};
int a[55][55];
bool check[55][55];
int main()
{
int t;
cin >> t;
while (t--)
{
int m, n, cnt;
cin >> m >> n >> cnt;
int x, y;
while (cnt--)
{
cin >> y >> x;
a[x][y] = 1;
}
int bug = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if ((a[i][j] == 1) && (check[i][j] == false))
{
check[i][j] = true;
queue<pair<int, int>> q;
q.push(make_pair(i, j));
while (!q.empty())
{
x = q.front().first;
y = q.front().second;
q.pop();
for (int k = 0; k < 4; k++)
{
int nx = x + dx[k];
int ny = y + dy[k];
if (nx >= 0 && nx < n && ny >= 0 && ny < m)
{
if (a[nx][ny] == 1 && check[nx][ny] == false)
{
check[nx][ny] = true;
q.push(make_pair(nx, ny));
}
}
}
}
bug++;
}
}
}
cout << bug << '\n';
memset(a, 0, sizeof(a));
memset(check, 0, sizeof(check));
}
return 0;
}
|
cs |