Skip to content

Conversation

@esc10946
Copy link
Contributor

@esc10946 esc10946 commented Jan 8, 2026

🚀 이슈 번호

Resolve: {#2284}

🧩 문제 해결

스스로 해결:

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지 : bfs
  • 🔹 어떤 방식으로 접근했는지 : 처음에는 bfs를 통해서 배열 범위 바깥으로 나갈 수 있는지 파악하고 나갈 수 없으면 해당 깊이를 구하려 했으나 물 높이를 정확하게 구할 수 없어서 인터넷에 검색을 하였다.
  • 위의 방식과 다른점은 가장자리에 임의의 칸을 놓고 bfs를 통해 같은 군집이 되는 지를 여부로 물이 바깥으로 흐르는 지를 파악했다.

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: O(NM)
  • 이유: bfs는 NM의 시간복잡도를 갖고 이후 반복문 또한 NM이라서

💻 구현 코드

#include <bits/stdc++.h>
#include <stdio.h> // scanf 사용을 위해 추가

using namespace std;

int direction[4][2] = { {-1,0}, {0,-1}, {1, 0}, {0, 1} };
const int MAX = 52;
int maximum = -1;

int arr[MAX][MAX];
queue<pair<int, int>> que;
int n, m, ans;

bool IsInside(int x, int y) {
    return x >= 0 && x <= n + 1 && y >= 0 && y <= m + 1;
}

void bfs(int height) {
    arr[0][0] = height;
    que.push({ 0, 0 });

    bool visited[MAX][MAX] = { false, };
    visited[0][0] = true;

    while (!que.empty())
    {
        int CurX = que.front().first;
        int CurY = que.front().second;
        que.pop();

        for (auto& dir : direction) {
            int nextX = CurX + dir[0];
            int nextY = CurY + dir[1];

            if (IsInside(nextX, nextY) && arr[nextX][nextY] < height) {
                arr[nextX][nextY] = height;  
                que.push({ nextX, nextY });
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            scanf_s("%1d", &arr[i][j]);
            maximum = max(maximum, arr[i][j]);
        }
    }

    for (int k = 1; k <= maximum; k++) {
        bfs(k);

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (arr[i][j] < k) {
                    ans += 1;
                    arr[i][j] += 1;
                }
            }
        }
    }

    cout << ans << endl;

    return 0;
}

@esc10946 esc10946 linked an issue Jan 8, 2026 that may be closed by this pull request
@esc10946 esc10946 self-assigned this Jan 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

260108 : 코딩테스트

2 participants