[BOJ / C++] 빙산 (BFS)

2025. 7. 23. 16:01·코딩테스트/BOJ

문제링크: https://www.acmicpc.net/problem/2573

 

1. 코드

#include <bits/stdc++.h>
using namespace std;

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

int iceCnt(vector<vector<int>>& board, int n, int m) {
    vector<vector<bool>> vis(n, vector<bool>(m, false));
    queue<pair<int, int>> Q;

    int cnt = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (board[i][j] == 0 || vis[i][j]) continue;

            vis[i][j] = true;
            Q.push({i, j});
            cnt++;
            while (!Q.empty()) {
                auto cur = Q.front(); Q.pop();
                int x, y;
                tie(x, y) = cur;
                for (int dir = 0; dir < 4; dir++) {
                    int nx = x + dx[dir];
                    int ny = y + dy[dir];
                    if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
                    if (board[nx][ny] <= 0 || vis[nx][ny] == true) continue;
                    Q.push({nx, ny});
                    vis[nx][ny] = true;
                }
            }
        }
    }
    return cnt;
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    int n, m;
    cin >> n >> m;

    vector<vector<int>> board(n, vector<int>(m, 0));

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> board[i][j];
        }
    }

    int year = 0;
    while (true) {
        if (iceCnt(board, n, m) >= 2) break;
        
        vector<vector<int>> tmp(board);
        year++;
        bool allmelt = true;
        for (int x = 0; x < n; x++) {
            for (int y = 0; y < m; y++) {
                if (tmp[x][y] >= 1) {
                    allmelt = false;
                    for (int dir = 0; dir < 4; dir++) {
                        int nx = x + dx[dir];
                        int ny = y + dy[dir];
                        
                        if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
                        if (tmp[nx][ny] == 0) {
                            if (board[x][y] == 0) continue;
                            else board[x][y] -= 1;
                        }
                    }
                }
            }
        }
        if (allmelt) {
            cout << 0;
            return 0;
        }
    }
    cout << year;
}

 

 

2. 분석

처음에 allmelt에 대한 처리를 하지 않아서 시간초과 판정이 났다. 모두 녹았을 때 0을 출력하는 로직을 따로 만들어줘야한다. 그리고 각 year 마다 주변 0의 개수를 세서 board[x][y]를 그만큼 잠식시키는 것이 이 문제의 독특한 점이다. 그렇게 어렵진 않고 전형적인 BFS 문제이다.

 

3. 시간복잡도

시간복잡도는 O(year * N * M)이다. 최대 n = 300, m = 300, year = 10이므로 year*n*m = 10 * 300 * 300 = 900,000 < 10^8 이므로 시간제한 1초 이내에 통과가 가능하다.

 

 

'코딩테스트 > BOJ' 카테고리의 다른 글

[BOJ / C++] 보물 (그리디)  (0) 2025.07.23
[BOJ / C++] 로프 (그리디)  (0) 2025.07.23
[BOJ / C++] 동전 0 (그리디)  (0) 2025.07.22
[BOJ / C++] 구간 합 구하기 4 (다이나믹 프로그래밍, prefix sum)  (0) 2025.07.22
[BOJ / C++] 2×n 타일링 (다이나믹 프로그래밍)  (0) 2025.07.22
'코딩테스트/BOJ' 카테고리의 다른 글
  • [BOJ / C++] 보물 (그리디)
  • [BOJ / C++] 로프 (그리디)
  • [BOJ / C++] 동전 0 (그리디)
  • [BOJ / C++] 구간 합 구하기 4 (다이나믹 프로그래밍, prefix sum)
sophon
sophon
sophon 님의 블로그 입니다.
  • sophon
    sophon 님의 블로그
    sophon
    • 카테고리 (174) N
      • 컴퓨터공학 (36)
        • 데이터베이스 (19)
        • 네트워크 (15)
        • 기타 이슈 (2)
      • 프로젝트 (17) N
        • Java (8)
        • Spring (4)
        • Docker (4)
        • AI Agent (1) N
      • 코딩테스트 (96)
        • BOJ (74)
        • 프로그래머스 (7)
        • 프로그래머스 SQL (13)
        • PS Snippets (2)
      • 🌱 잡담 (22)
        • 자격증 (7)
        • 좋은 시 모음 (12)
        • 책과 영화 (3)
        • 기록 (0)
  • 전체
    오늘
    어제
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
sophon
[BOJ / C++] 빙산 (BFS)
상단으로

티스토리툴바