๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ Algorithm/์•Œ๊ณ ๋ฆฌ์ฆ˜-Swift

[99ํด๋Ÿฝ ์ฝ”ํ…Œ ์Šคํ„ฐ๋”” 4์ผ์ฐจ TIL] ๋ฐฑ์ค€ ์•ˆ์ „ ์˜์—ญ Swift

by Danna 2025. 4. 3.
728x90
728x90

์˜ค๋Š˜์˜ ๋ฌธ์ œ

- ๋ฐฑ์ค€ ์•ˆ์ „ ์˜์—ญ

- https://www.acmicpc.net/problem/2468

 

์˜ค๋Š˜์˜ ํ•™์Šต ํ‚ค์›Œ๋“œ

- BFS(๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰), DFS(๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰)

- BFS + Queue

- DFS + Stack

 

BFS (๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰)

- ์ตœ๋‹จ ๊ฑฐ๋ฆฌ, ์ตœ์†Œ ํšŸ์ˆ˜, ์ตœ์†Œ ๋น„์šฉ ๊ฐ™์€ ์ตœ์†Œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ•  ๋•Œ ์ž์ฃผ ์‚ฌ์šฉ๋œ๋‹ค.

- ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ํ•œ ๋‹จ๊ณ„์”ฉ ํƒ์ƒ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๋ฌธ์ œ์—์„œ ์œ ๋ฆฌํ•˜๋‹ค.

- Queue ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ™์€ ๋ ˆ๋ฒจ ์ˆœ์œผ๋กœ ํƒ์ƒ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

- ๊ทธ๋ž˜ํ”„์—์„œ ์—ฐ๊ฒฐ๋œ ์˜์—ญ์„ ํƒ์ƒ‰ํ•˜๋Š” ๋ฌธ์ œ์—์„œ๋„ ์œ ์šฉํ•˜๋‹ค.

- ์„ฌ์˜ ๊ฐœ์ˆ˜ ์ฐพ๊ธฐ, ๊ฐ์—ผ๋œ ์˜์—ญ ๊ตฌํ•˜๊ธฐ, ์ƒ‰์น ๋œ ์˜์—ญ ๊ตฌํ•˜๊ธฐ ๋“ฑ ..

 

DFS (๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰)

- ๊ทธ๋ž˜ํ”„๋‚˜ ํŠธ๋ฆฌ์—์„œ ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๋๊นŒ์ง€ ํƒ์ƒ‰ํ•œ ํ›„, ๋‹ค์‹œ ๋Œ์•„์™€ ๋‹ค๋ฅธ ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

- ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๊นŠ์ด ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— "์ตœ์†Œ ๊ฑฐ๋ฆฌ"๋ฅผ ๋ณด์žฅํ•˜๊ธฐ ์–ด๋ ต๋‹ค.

- ์Šคํƒ์ด๋‚˜, ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค.

- ๊ฒฝ๋กœ ๊ฐœ์ˆ˜ ์ฐพ๊ธฐ, ์™„์ „ ํƒ์ƒ‰, ๋ฐฑํŠธ๋ž˜ํ‚น(์ตœ์ ์˜ ์กฐํ•ฉ ์ฐพ๊ธฐ) ๊ฐ™์€ ๋ฌธ์ œ์— ๋งŽ์ด ์“ฐ์ธ๋‹ค.

 

์ ‘๊ทผ ๋ฐฉ๋ฒ•

BFS vs DFS

- ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๊ทธ๋ž˜ํ”„์—์„œ ์—ฐ๊ฒฐ๋œ ์˜์—ญ (= ๋น—๋ฌผ์ด ๋„˜์น˜์ง€ ์•Š๋Š” ์•ˆ์ „ ์˜์—ญ)์„ ์ฐพ๊ธฐ ๋•Œ๋ฌธ์— BFS ๋กœ ์ ‘๊ทผํ•œ๋‹ค.

- BFS ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•œ ์˜์—ญ์„ ๋ชจ๋‘ ํƒ์ƒ‰ํ•œ ํ›„์— ์ƒˆ๋กœ์šด ์˜์—ญ์„ ์ฐพ์•„์„œ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

- DFS ๋„ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, BFS ๋Š” ํ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•˜๊ธฐ ์‰ฝ๊ณ  ์ง๊ด€์ ์ด๋‹ค.

 

Queue

- Swift ์—์„œ๋Š” Queue ๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ฐ˜ List ๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ–ˆ๋‹ค.

- First In First Out ์œผ๋กœ ๋™์ž‘ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ธ์ž๋ฅผ ๊บผ๋‚ผ๋•Œ๋Š” removeFirst() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

- Queue ๋Š” O(1) ์ด์ง€๋งŒ List ์—์„œ๋Š” (n) ์˜ ์ž‘์—…์ด๋‹ค.

- ์ธ์ž๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ์—๋Š” append() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , O(1) ์˜ ์ž‘์—…์ด๋‹ค.

 

๋ฌธ์ œ ํ’€์ด

func calculateSafeArea(n: Int, h: Int, originalMap: [[Int]]) -> Int {
    var visitMap = originalMap
    var queue = [(Int, Int)]() // ๊ฐ€๊นŒ์šด ๋…ธ๋“œ๋ฅผ ๋จผ์ € ๋ฐฉ๋ฌธํ•ด์•ผํ•ด์„œ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•ด์•ผํ•จ
    var count = 0
    let dy = [-1, 1, 0, 0]
    let dx = [0, 0, -1, 1]

    for y in 0..<n {
        for x in 0..<n {
            // ํ˜„์žฌ ๋ฌผ์— ์ž ๊ธฐ์ง€ ์•Š๋Š” ์ง€์ 
            guard visitMap[y][x] > h else { continue }
            count += 1 // ๋ฌผ์— ์ž ๊ธฐ์ง€ ์•Š๋Š” ์˜์—ญ
            visitMap[y][x] = 0 // ๋‹ค๋ฅธ ์ง€์ ์—์„œ ๋ฐฉ๋ฌธํ•  ๋•Œ ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ์Œ์„ ์ฒดํฌ
            queue.append((y, x)) // ํ˜„์žฌ ์ง€์ ๋ถ€ํ„ฐ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด ํ์— ์ถ”๊ฐ€

            while queue.isEmpty == false {
                let (cy, cx) = queue.removeFirst() // O(n)

                for m in 0..<4 {
                    let ny = cy + dy[m]
                    let nx = cx + dx[m]
                    guard ny >= 0, ny < n, nx >= 0, nx < n,  // index ์ฒดํฌ
                          visitMap[ny][nx] > h else { continue } // ์ฃผ๋ณ€์ด ๋ฌผ์— ์ž ๊ธฐ์ง€ ์•Š๋Š” ์ง€์ ์ธ์ง€ ์ฒดํฌ
                    visitMap[ny][nx] = 0
                    queue.append((ny, nx))
                }
            }
        }
    }

    return count
}

func readInput() -> (Int, Int, [[Int]])? {
    guard let input = readLine(), let n = Int(input) else { return nil }
    var inputArray = [[Int]]()
    var maxItem = Int.min

    for _ in 0..<n {
        guard let input = readLine() else { continue }
        inputArray.append(input.compactMap {
            guard let item = Int(String($0)) else { return nil }
            maxItem = max(maxItem, item)
            return item
        })
    }


    return (n, maxItem, inputArray)
}

if let (n, maxHeight, originalMap) = readInput() {
    var result = Int.min

    for h in 0..<maxHeight {
        let count = calculateSafeArea(n: n, h: h, originalMap: originalMap)
        result = max(result, count)
    }

    print(result)
}

 

- ๋ฐ˜๋ก€ ๊ธฐ์ค€์œผ๋กœ ์ „๋ถ€ ์ฒดํฌํ–ˆ๋Š”๋ฐ ๊ฒฐ๊ณผ๊ฐ€ ํ‹€๋ฆผ์œผ๋กœ ๋‚˜์˜จ๋‹ค

 

 

์˜ค๋Š˜์˜ ํšŒ๊ณ 

- DFS, BFS ๊ธฐ๋ณธ์ ์ธ ๋ฌธ์ œ๋ฅผ ๋” ํ’€์–ด๋ณด๊ธฐ

- Deque (Double-ended Queue) ๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ

- Swift ์—์„œ๋Š” ArraySlice ๋˜๋Š” LinkedList ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

728x90
728x90