본문 바로가기

그래프

유기농 배추

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[] = { 001-1 };
int dy[] = { 1-100};
 
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<intint>> 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, 0sizeof(a));
        memset(check, 0sizeof(check));
    }
    return 0;
}
    
 
 
 
 
 
 
 
 
cs

'그래프' 카테고리의 다른 글

게임 개발  (0) 2023.09.04
위상 정렬  (0) 2023.09.03
소문난 칠공주  (0) 2023.03.14
서울 지하철 2호선 (DFS + BFS)  (0) 2023.03.06
단지번호붙이기  (0) 2023.03.03