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

๋ฌธ์ž์—ด ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฌธ์ œ. ๊ตฌ๋ฆ„์ด ์žˆ๋˜ ์ž๋ฆฌ๋‚˜, ์ด๋ฏธ ์ง€๋‚˜๊ฐ„ ์ž๋ฆฌ๋Š” ์ฒ˜๋ฆฌํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ตฌ๋ฆ„์ด ์—†๋˜ ์ž๋ฆฌ๋ฅผ ๋ชจ๋‘ "-1"๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ํ•œ๋ฒˆ๋„ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ๋งŒ ์‹œ๊ฐ„์„ ๋„ฃ์œผ๋ฉด ๋œ๋‹ค.

 

๋ฌธ์ œ

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

 

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

Swift

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

var clouds = [[Int]]()

for i in 0..<size[0] {
    for j in 0..<size[1] {
        if board[i][j] == "." {
            board [i][j] = "-1"
        } else {
            board[i][j] = "0"
            clouds.append([i, j])
        }
    }
}

var time = 1
while !clouds.isEmpty {
    var temp = [[Int]]()
    while !clouds.isEmpty {
        var cloud = clouds.removeFirst()
        cloud[1] += 1
        if cloud[1] >= size[1] { // ๊ตฌ๋ฆ„์ด ๋ฐ”๊นฅ์œผ๋กœ ๋ฒ—์–ด๋‚ฌ์„ ๋•Œ
            continue
        } else { // ๊ตฌ๋ฆ„์ด ๋ฐ”๊นฅ์œผ๋กœ ๋ฒ—์–ด๋‚˜์ง€ ์•Š์Œ
            if board[cloud[0]][cloud[1]] == "-1" { // ํ•œ๋ฒˆ๋„ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ
                board[cloud[0]][cloud[1]] = "\(time)"
                temp.append(cloud)
            }
        }
    }

    clouds = temp
    time += 1
}

for b in board {
    print(b.joined(separator: " "))
}
๋ฐ˜์‘ํ˜•