프로그래밍 공부

BOJ/백준 1012번 유기농 배추 본문

Problem Solving/Baekjoon Online Judge

BOJ/백준 1012번 유기농 배추

khj1999 2020. 10. 23. 17:56
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
#include <iostream>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;
 
int arr[101][101];
bool vis[101][101];
int dx[4= { 1,0,-1,0 };
int dy[4= { 0,1,0,-1 };
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        int cnt = 0;
        queue<pair<intint>> Q;
        int m, n, k;
        cin >> m >> n >> k;
        for (int reset = 0; reset < m; reset++) fill(arr[reset], arr[reset] + n, 0);
        for (int reset = 0; reset < m; reset++) fill(vis[reset], vis[reset] + n, false);
        for (int j = 0; j < k; j++) {
            int x, y;
            cin >> x >> y;
            if(x <= m - 1 && y <= n - 1) arr[y][x] = 1;
        }
        for (int x = 0; x <= n - 1; x++) {
            for (int y = 0; y <= m - 1; y++) {
                if (arr[x][y] == 1 && vis[x][y] == false) {
                    cnt++;
                    Q.push({ x, y });
                    vis[x][y] = true;
                    while (!Q.empty()) {
                        auto cur = Q.front(); Q.pop();
                        for (int dir = 0; dir < 4; dir++) {
                            int nx = cur.first + dx[dir];
                            int ny = cur.second + dy[dir];
                            if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
                            if (vis[nx][ny] || arr[nx][ny] != 1continue;
                            vis[nx][ny] = true;
                            Q.push({ nx,ny });
                        }
                    }
                }
            }
        }
        cout << cnt << '\n';
    }
}
cs

'Problem Solving > Baekjoon Online Judge' 카테고리의 다른 글

BOJ/백준 17496번 스타후르츠  (0) 2021.04.11
BOJ/백준 17478번 재귀함수가 뭔가요?  (0) 2020.10.29
BOJ/백준 2576번 홀수  (0) 2019.05.16
BOJ/백준 2566번 최댓값  (0) 2019.05.16
BOJ/백준 2857번 FBI  (0) 2019.05.16
Comments