Skip to content

Conversation

@alirz-pixel
Copy link
Member

🚀 이슈 번호

Resolve: {#2284}

🧩 문제 해결

스스로 해결:

🔎 접근 과정

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

  • 🔹 어떤 알고리즘을 사용했는지: BFS
  • 🔹 어떤 방식으로 접근했는지

입력값 외부에 0으로 된 border를 채운다.
이후 board 전역에 비가 내린다고 가정하여 높이가 1부터 max까지 차오른다고 해보자.

이제 각 높이에 따라 0,0부터 bfs를 진행하면서
현재 높이보다 낮은 곳들을 방문해나간다.
(방문한 곳은 현재 높이로 채우기)

해당 방식으로 bfs를 진행하고 난 이후에
현재 높이보다 낮은 board 값은 물이 차오르지 않은 영역 (벽으로 둘러쌓인 영역) 이므로
수영장 내부를 의미하게 된다.

⏱️ 시간 복잡도

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

  • Big-O 표기법: $O((NM)^2 * H)$
  • 이유:: 물이 차오르는 높이마다 BFS 및 board 탐색을 진행하기 때문이다.

💻 구현 코드

#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int dy[4] = { 1, -1, 0, 0 };
int dx[4] = { 0, 0, 1, -1 };
void BFS(vector<vector<int>> &board, int h) {
    vector<vector<bool>> visited(board.size(), vector<bool>(board[0].size(), false));
    queue<pair<int, int>> q;

    q.push({ 0, 0 });
    visited[0][0] = true;
    board[0][0] = h;
    while (!q.empty()) {
        pair<int, int> front = q.front();
        q.pop();

        for (int dir = 0; dir < 4; dir++) {
            int ny = front.first + dy[dir];
            int nx = front.second + dx[dir];

            if (ny < 0 || nx < 0 || ny >= board.size() || nx >= board[0].size() || visited[ny][nx])
                continue;
            visited[ny][nx] = true;

            if (board[ny][nx] < h) {
                q.push({ ny, nx });
                board[ny][nx] = h;
            }
        }
    }
}

void print(vector<vector<int>> &board) {
    for (int y = 0; y < board.size(); y++) {
        for (int x = 0; x < board[0].size(); x++) {
            cout << board[y][x] << " ";
        }
        cout << "\n";
    }
    cout << "\n";
}

int main() {
    int N, M;
    cin >> N >> M;

    int maxV = 0;
    vector<vector<int>> board(N + 2, vector<int>(M + 2));
    for (int y = 1; y <= N; y++) {
        string input;
        cin >> input;
        for (int x = 0; x < M; x++) {
            board[y][x + 1] = input[x] - '0';
            maxV = max(maxV, board[y][x + 1]);
        }
    }
    // print(board);

    int answer = 0;
    for (int h = 1; h <= maxV; h++) {
        BFS(board, h);
        // print(board);

        for (int y = 1; y <= N; y++) {
            for (int x = 1; x <= M; x++) {
                if (board[y][x] < h) {
                    answer += 1;
                    board[y][x] = h;
                }
            }
        }
    }

    cout << answer;

    return 0;
}

@alirz-pixel alirz-pixel self-assigned this Jan 8, 2026
@alirz-pixel alirz-pixel linked an issue Jan 8, 2026 that may be closed by this pull request
@alirz-pixel alirz-pixel merged commit 368158a into main Jan 12, 2026
1 check passed
@alirz-pixel alirz-pixel deleted the munhyeong/2284/1 branch January 12, 2026 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

260108 : 코딩테스트

2 participants