๊ธ€ ์ž‘์„ฑ์ž: ์ด์ง€์›๐ŸŒฉ๏ธ

27:09

๋‘ ๋‹ฌ ์ „์— ์‹œ๋„ํ–ˆ์„ ๋•Œ๋Š” ๋ชป ํ’€์—ˆ๋Š”๋ฐ, ์˜ค๋Š˜ ๋‹ค์‹œ ํ’€์—ˆ๋Š”๋ฐ ํ†ต๊ณผํ–ˆ๋‹ค. ๊ฐ๋™์˜ ๋ˆˆ๋ฌผ…. ๋”ฐํํ‘.
๋…น์ด๊ณ , ๋ฉ์–ด๋ฆฌ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์„œ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค. ์ด์ œ๋ณด๋‹ˆ, ์˜ˆ์ „์˜ ์ฝ”๋“œ ๋Œ€๋น„ 2๋ฐฐ ์ •๋„ ์ค„์–ด๋“ค์—ˆ๋‹ค. ใ…‡0ใ…‡


๋ฌธ์ œ

https://www.acmicpc.net/problem/2573


๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

Swift

let size = readLine()!.split(separator: " ").map { Int(String($0))! }
let col = size[0]
let row = size[1]
let direction = [(1, 0), (0, 1), (-1, 0), (0, -1)]
var board = [[Int]]()
for _ in 0..<col {
    let line = readLine()!.split(separator: " ").map { Int(String($0))! }
    board.append(line)
}

// ๋…น์ด๊ธฐ
func melting() {
    var melt = Array(repeating: Array(repeating: 0, count: row), count: col)
    for i in 0..<col {
        for j in 0..<row {
            if board[i][j] != 0 {
                for k in 0..<4 {
                    let x = i + direction[k].0
                    let y = j + direction[k].1
                    if board[x][y] == 0 {
                        melt[i][j] += 1
                    }
                }
            }
        }
    }
    for i in 0..<col {
        for j in 0..<row {
            board[i][j] = board[i][j] - melt[i][j] < 0 ? 0 : board[i][j] - melt[i][j]
        }
    }
}

// ๋ฉ์–ด๋ฆฌ ์ฐพ๊ธฐ
func bfs() -> Int {
    var area = board
    var count = 0

    for i in 0..<col {
        for j in 0..<row {
            if area[i][j] != 0 {
                var queue = [(i, j)]
                area[i][j] = 0
                while !queue.isEmpty {
                    let v = queue.removeLast()
                    for k in 0..<4 {
                        let x = v.0 + direction[k].0
                        let y = v.1 + direction[k].1

                        if !(0..<col ~= x) || !(0..<row ~= y) || area[x][y] == 0 {
                            continue
                        }
                        queue.append((x, y))
                        area[x][y] = 0
                    }
                }
                count += 1
            }
        }
    }
    return count
}

var time = 0
var count = 0
while true {
    count = bfs()
    if count >= 2 {
        break
    } else if count == 0 {
        break
    }

    melting()

    time += 1
}
print(count == 0 ? 0 : time)
๋ฐ˜์‘ํ˜•