Home CodeTree 토스트 계란틀
Post
Cancel

CodeTree 토스트 계란틀

토스트 계란틀 [골드 5]


문제 링크

https://www.codetree.ai/training-field/frequent-problems/problems/toast-eggmold/description?page=1&pageSize=20

풀이

문제를 잘못 읽어서 하루 넘게 고생했다… 문제를 꼼꼼히 잘 읽어보자! 한 번 반복 시 해당 계란틀을 모두 합치는 게 아니라 벽을 공유하는 것 끼리만 합쳐야 한다.

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <queue>
#include <stdlib.h>
#include <vector>

using namespace std;

int n, L, R;
int eggs[51][51];
bool isVisited[51][51];

int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};

int answer = 0;

void printEggs()
{
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cout << eggs[i][j] << " ";
        }
        cout << "\n";
    }

    cout << "\n\n";
}

int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> L >> R;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cin >> eggs[i][j];
        }
    }

    bool check = true;
    while (check)
    {
        // printEggs();
        check = false;
        fill_n(&isVisited[0][0], 51 * 51, false);

        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (!isVisited[i][j])
                {
                    vector<pair<int, int>> list;

                    queue<pair<int, int>> q;
                    q.push({i, j});

                    while (!q.empty())
                    {
                        auto elem = q.front();
                        q.pop();

                        int y = elem.first;
                        int x = elem.second;
                        int val = eggs[y][x];

                        int cnt = 0;
                        isVisited[y][x] = true;

                        for (int k = 0; k < 4; k++)
                        {
                            int ny = y + dy[k];
                            int nx = x + dx[k];

                            if (0 < ny && ny <= n && 0 < nx && nx <= n)
                            {
                                int diff = abs(val - eggs[ny][nx]);
                                if (L <= diff && diff <= R)
                                {
                                    cnt++;
                                    if (!isVisited[ny][nx])
                                    {
                                        isVisited[ny][nx] = true;
                                        q.push({ny, nx});
                                    }
                                }
                            }
                        }

                        if (cnt > 0)
                        {
                            list.push_back({y, x});
                            check = true;
                        }
                    }

                    if (list.size() > 0)
                    {
                        int sum = 0;

                        for (int i = 0; i < list.size(); i++)
                        {
                            sum += eggs[list[i].first][list[i].second];
                        }

                        sum = sum / (int)list.size();

                        for (int i = 0; i < list.size(); i++)
                        {
                            eggs[list[i].first][list[i].second] = sum;
                        }
                    }
                }
            }
        }

        if (check)
            answer++;
    }

    cout << answer;
}
}
This post is licensed under CC BY 4.0 by the author.