algorithm/BOJ

BOJ 2638번 치즈

_JunHo 2020. 2. 13. 13:41

BOJ : https://www.acmicpc.net/problem/2638

github : https://github.com/junho0956/Algorithm/blob/master/2638/2638/%EC%86%8C%EC%8A%A4.cpp

 

2636번 치즈에서 조건이 2개의 공기와 마주친다는 점만 다릅니다.

이외에는 구현이 비슷합니다.

2개의 공기와 마주치는 점들을 따로 큐에 모아놓고, 그 점들을 삭제하면서 구멍과 마주치는 부분이 있는지

확인하였습니다.

 

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <queue>
using namespace std;
 
typedef pair<intint> pii;
int arr[101][101];
int n, m;
bool visit[101][101];
int my[4= { -1,1,0,0 };
int mx[4= { 0,0,1,-1 };
queue<pair<pii, int> > q;
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m;
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> arr[i][j];
            if (arr[i][j]) q.push({ {i,j},0 });
        }
    }
 
    queue<pair<intint> > out;
    bool check = false;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (arr[i][j] == 0) {
                check = true;
                out.push({ i,j });
                while (!out.empty()) {
                    int y = out.front().first;
                    int x = out.front().second;
                    out.pop();
                    if (visit[y][x]) continue;
                    visit[y][x] = true;
                    arr[y][x] = 2;
                    for (int i = 0; i < 4; i++) {
                        int yy = y + my[i];
                        int xx = x + mx[i];
                        if (yy >= 0 && yy < n && xx >= 0 && xx < m && !visit[yy][xx] && !arr[yy][xx]) {
                            out.push({ yy,xx });
                        }
                    }
                }
                break;
            }
        }
        if (check) break;
    }
 
    queue<pii> hole;
    int time = 0;
    while (1) {
        int del = 0;
        while (!q.empty()) {
            int y = q.front().first.first;
            int x = q.front().first.second;
            int cnt = q.front().second;
            if (time != cnt) break;
            q.pop();
            int out_cnt = 0;
            for (int i = 0; i < 4; i++) {
                int yy = y + my[i];
                int xx = x + mx[i];
                if (yy >= 0 && yy < n && xx >= 0 && xx < m && arr[yy][xx] == 2) {
                    out_cnt++;
                }
            }
            if (out_cnt >= 2hole.push({ y,x });
            else q.push({ {y,x},cnt + 1 });
        }
 
        while (!hole.empty()) {
            int y = hole.front().first;
            int x = hole.front().second;
            hole.pop();
            arr[y][x] = 2;
            del++;
            for (int i = 0; i < 4; i++) {
                int yy = y + my[i];
                int xx = x + mx[i];
                if (yy >= 0 && yy < n && xx >= 0 && xx < m && !arr[yy][xx]) out.push({ yy,xx });
            }
        }
 
        while (!out.empty()) {
            int y = out.front().first;
            int x = out.front().second;
            out.pop();
            if (visit[y][x]) continue;
            visit[y][x] = true;
            arr[y][x] = 2;
            for (int i = 0; i < 4; i++) {
                int yy = y + my[i];
                int xx = x + mx[i];
                if (yy >= 0 && yy < n && xx >= 0 && xx < m && !arr[yy][xx]) out.push({ yy,xx });
            }
        }
 
        if (!del) break;
        time++;
    }
 
    cout << time;
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter