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

์ฃผ์–ด์ง€๋Š” ์™ผ์ชฝ ์•„๋ž˜ ๊ผญ์ง“์ , ์˜ค๋ฅธ์ชฝ ์œ„ ๊ผญ์ง“์ ์˜ ๊ฐ’์„ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋กœ ์น˜ํ™˜ํ•˜๋Š” ๊ณผ์ •์„ ๋ฐ”๋กœ ์ƒ๊ฐํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์›Œ์„œ ์†์œผ๋กœ ์—ด์‹ฌํžˆ ๊ณ„์‚ฐํ•˜๋ฉด์„œ ํ’€์—ˆ๋‹ค. ์ด ๊ณผ์ • ์ดํ›„์—๋Š” ํƒ์ƒ‰ ๋ฌธ์ œ์™€ ๋™์ผํ•˜๊ฒŒ ํ’€๋ฉด ๋œ๋‹ค. ๊ฐ ์˜์—ญ์˜ ๋„“์ด๋Š” visited์˜ ํฌ๊ธฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋˜๋Š”๋ฐ, ์ด ๋•Œ ์™œ์ธ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ ์ค‘๋ณต๊ฐ’์ด ๋“ค์–ด๊ฐ€์„œ ํฌ๊ธฐ๊ฐ€ ๋งž์ง€ ์•Š์•„ visited๋ฅผ Set์œผ๋กœ ์„ ์–ธํ•˜์—ฌ ์ค‘๋ณต๊ฐ’ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์คฌ๋‹ค.

 

๋ฌธ์ œ

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

 

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

Swift

let n = readLine()!.split(separator: " ").map { Int(String($0))! }
var board = Array(repeating: Array(repeating: 0, count: n[1]), count: n[0])

for _ in 0..<n[2] {
    let area = readLine()!.split(separator: " ").map { Int(String($0))! }
    let startX = n[0] - area[1] - 1
    let startY = area[0]
    let endX = n[0] - area[3]
    let endY = area[2] - 1

    for i in endX...startX {
        for j in startY...endY {
            board[i][j] = 1
        }
    }
}

var area = [Int]()
let dx = [1, 0, -1, 0]
let dy = [0, 1, 0, -1]
var count = 0
for i in 0..<n[0] {
    for j in 0..<n[1] {
        if board[i][j] == 0 {
            var visited = Set<[Int]>()
            visited.insert([i, j])
            var queue = [[i, j]]
            while !queue.isEmpty {
                let v = queue.removeFirst()
                for (x, y) in zip(dx, dy) {
                    let nx = x + v[0]
                    let ny = y + v[1]
                    if nx < 0 || nx >= n[0] || ny < 0 || ny >= n[1] || board[nx][ny] == 1 {
                        continue
                    }
                    if board[nx][ny] == 0 {
                        visited.insert([nx, ny])
                        queue.append([nx, ny])
                        board[nx][ny] = 1
                    }
                }
            }
            area.append(visited.count)
            count += 1
        }
    }
}

print(count)
print(area.sorted().map({ String($0) }).joined(separator: " "))
๋ฐ˜์‘ํ˜•