๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ–ฅ CS/Algorithm

[Algorithm/Swift] BOJ(๊ณจ๋“œ4) - 2573 ๋น™์‚ฐ

์•ˆ๋…•ํ•˜์„ธ์š” ์ œ์ธ์ž…๋‹ˆ๋‹ค :)

PS ๊ธ€์€ ์˜ค๋žœ๋งŒ์ธ ๊ฒƒ ๊ฐ™์€๋ฐ.. ์˜ค๋Š˜์€ ๋ฐฑ์ค€ ๋ฌธ์ œ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!

BFS ๋ฌธ์ œ์ธ๋ฐ ํ•œ๋ฒˆ ๋” ์ƒ๊ฐํ•ด์•ผ ๋  ๋ถ€๋ถ„์ด ์žˆ๋Š” ๋ฌธ์ œ์—ฌ์„œ ์ •๋ฆฌํ•ด๋‘๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์„œ ์ฒ˜์Œ์œผ๋กœ ๋ฐฑ์ค€ ๋ฌธ์ œ๋ฅผ ๊ฐ€์ ธ์™€๋ดค์Šต๋‹ˆ๋‹ค.

๋ฐ”๋กœ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค !

๋ฌธ์ œ ๋งํฌ

 

2573๋ฒˆ: ๋น™์‚ฐ

์ฒซ ์ค„์—๋Š” ์ด์ฐจ์› ๋ฐฐ์—ด์˜ ํ–‰์˜ ๊ฐœ์ˆ˜์™€ ์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ์ •์ˆ˜ N๊ณผ M์ด ํ•œ ๊ฐœ์˜ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. N๊ณผ M์€ 3 ์ด์ƒ 300 ์ดํ•˜์ด๋‹ค. ๊ทธ ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ์ค„๋งˆ๋‹ค ๋ฐฐ์—ด์˜ ๊ฐ ํ–‰์„

www.acmicpc.net

๋ฌธ์ œ ํ’€์ด

๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฌธ์ œ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ •๋ฆฌํ•ด๋ณด์ž๋ฉด, 2์ฐจ์› ๋ฐฐ์—ด์— ๋น™์‚ฐ์˜ ๊ฐ ๋ถ€๋ถ„๋ณ„ ๋†’์ด ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, ์ธ์ ‘ํ•œ ์นธ์ด 0์ผ ๊ฒฝ์šฐ(๊ทธ๋ฆผ์—์„œ ๋นˆ์นธ), ๋น™์‚ฐ์ด ์ผ๋…„๋งˆ๋‹ค ๊ทธ ์นธ์— ์ธ์ ‘ํ•œ ๋นˆ์นธ์˜ ๊ฐœ์ˆ˜๋งŒํผ ์ค„์–ด๋“ญ๋‹ˆ๋‹ค. ๊ตฌํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€ ์ฃผ์–ด์ง„ ๊ทธ๋ž˜ํ”„๊ฐ€ ๋‘ ๊ฐœ ์ด์ƒ์œผ๋กœ ๋ถ„๋ฆฌ๋˜๋Š” ๋ฐ๊นŒ์ง€ ๊ฑธ๋ฆฌ๋Š” ์ตœ์†Œ ์‹œ๊ฐ„(๋…„)์ด์—ˆ์Šต๋‹ˆ๋‹ค.

 

์ธ์ ‘ํ•œ ๋…ธ๋“œ ์ค‘ 0์ธ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด ํ•ด๋‹น ์นธ์ด 0์ด ๋  ๋•Œ๊นŒ์ง€ ๊ณ„์‚ฐํ•˜๊ณ , ๊ทธ๋ž˜ํ”„๊ฐ€ ์–ธ์ œ ๋‘ ๊ฐœ๋กœ ๋ถ„๋ฆฌ๋˜๋Š”์ง€ ๊ตฌํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ด๊ธฐ ๋•Œ๋ฌธ์— BFS๋กœ ํ’€๋ฉด ๋˜๊ฒ ๋‹ค๋Š” ์•„์ด๋””์–ด๊ฐ€ ๋ฐ”๋กœ ๋– ์˜ฌ๋ž๋Š”๋ฐ์š”,

 

1. BFS๋กœ ์ธ์ ‘ํ•œ 0์˜ ๊ฐœ์ˆ˜ ํƒ์ƒ‰

2. ๋น™์‚ฐ ๋…น์ด๊ธฐ

3. ๋‘ ๋ฉ์–ด๋ฆฌ๋กœ ๋‚˜๋‰˜๊ฑฐ๋‚˜, ๋ชจ๋“  ๋น™์‚ฐ์ด 0์ด ๋˜๋ฉด ๊ฒฐ๊ณผ๊ฐ’ ์ถœ๋ ฅ ํ›„ ์ข…๋ฃŒ

 

์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„์„ ํ•ด์„œ ๋Œ๋ ค๋ดค๋Š”๋ฐ.. ์ƒ๊ฐํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š๋”๋ผ๊ตฌ์š”..??

๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ํ’€์ด๋“ค์„ ์ฐพ์•„๋ดค๋Š”๋ฐ,, ์ œ๊ฐ€ ๋†“์นœ ๋ถ€๋ถ„์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค..

(1, 1) ์œ„์น˜์˜ ๋…ธ๋“œ์— ์ธ์ ‘ํ•œ ๋นˆ์นธ์€ 2๊ฐœ(๋…ธ๋ž€์ƒ‰ ๋ถ€๋ถ„)์ด๋ฏ€๋กœ, 1๋…„์ด ์ง€๋‚˜๋ฉด 0์ด ๋ฉ๋‹ˆ๋‹ค. (1, 2) ์œ„์น˜์˜ ๋…ธ๋“œ๋„ ์ธ์ ‘ํ•œ ๋นˆ์นธ์ด 2๊ฐœ(ํŒŒ๋ž€์ƒ‰ ๋ถ€๋ถ„) 1๋…„์ด ์ง€๋‚˜๋ฉด -2๊ฐ€ ๋˜์–ด 2๊ฐ€ ๋˜๊ณ , ๋˜ 1๋…„์ด ์ง€๋‚˜๋ฉด ๋˜ -2๊ฐ€ ๋˜์–ด 2๋…„ ํ›„์— 0์ด ๋˜๋Š” ๊ฒƒ์ด ๋งž๊ฒ ์ฃ ??

 

ํ•˜์ง€๋งŒ,, ์ œ๊ฐ€ ์ง  ๋กœ์ง์—์„œ๋Š” ๋ฐ”๋กœ ์˜† ๋…ธ๋“œ์ธ (1, 1) ๋…ธ๋“œ๊ฐ€ 1๋…„ ํ›„ 0์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— 2๋…„์งธ ๋˜๋Š” ํ•ด์— (1, 2) ๋…ธ๋“œ์˜ ์ธ์ ‘ํ•œ ๋นˆ์นธ ๊ฐœ์ˆ˜๊ฐ€ 3๊ฐœ๋กœ ๊ณ„์‚ฐ์ด ๋˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.. ํ•ด๊ฐ€ ๊ฐˆ์ˆ˜๋ก ์ ์  0์ด ๋˜๋Š” ์นธ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ ๊ฒฐ๊ณผ ๊ฐ’์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ƒ๊ฐ์„ ํ•˜์ง€ ๋ชปํ•ด์„œ ๊ฒฐ๊ณผ๊ฐ€ ์ƒ๊ฐํ•œ ๊ฒƒ๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ค๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ๋Š”๋ฐ์š”, ๊ทธ๋ ‡๋‹ค๋ฉด ์ดˆ๊ธฐ ๋ฐฐ์—ด์„ ์ €์žฅํ•ด๋‘๊ณ  ํ•ด๋‹น ๋ฐฐ์—ด๊ณผ ๋น„๊ตํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์ด๋ฅผ ํ•˜๋ฉด ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ ๊ฐ™๋„ค์š”!

 

โญ๏ธ ์ด ๋ฌธ์ œ์˜ ๊ตฌํ˜„ ํฌ์ธํŠธ โญ๏ธ

โŒ ๊ทธ๋ž˜ํ”„์˜ ํ˜„ ์ƒํƒœ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋น™์‚ฐ์„ ๋…น์ด๋ฉด ์•ˆ๋จ

โญ•๏ธ ๋น™์‚ฐ์„ ์–ผ๋งˆ๋‚˜ ๋…น์—ฌ์•ผ ๋˜๋Š”์ง€ ๊ณ„์‚ฐํ•  ๋•Œ, ์ดˆ๊ธฐ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ณ„์‚ฐํ•˜๊ธฐ

Swift ํ’€์ด ์ฝ”๋“œ

import Foundation

let directions = [(0, 1), (1, 0), (-1, 0), (0, -1)]
let input = readLine()!.split(separator: " ").map {Int($0)!}
let (n, m) = (input[0], input[1])
var arr: [[Int]] = [] // ๋น™์‚ฐ ๋†’์ด ์ •๋ณด ๋‹ด๋Š” 2์ฐจ์› ๋ฐฐ์—ด
var visited: [[Int]] = Array(repeating: Array(repeating: 0, count: m), count: n) // ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ์ฒดํฌ (๋ช‡๋ฒˆ์งธ ๋…„๋„์— ๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€)

// ์ฃผ์–ด์ง„ ๋น™์‚ฐ์˜ ๋†’์ด ์ •๋ณด arr์— ์ €์žฅ
for _ in 0..<n {
    let row = readLine()!.split(separator: " ").map {Int($0)!}
    arr.append(row)
}

var count = 1 // ๋ฉ์–ด๋ฆฌ ๊ฐœ์ˆ˜
var answer = 0 // ๋ช‡๋ฒˆ์งธ ํ•ด์ธ์ง€ ์ €์žฅ

func bfs(_ graph: inout [[Int]], _ x: Int, _ y: Int) {
    var queue = [(x, y)]
    while !queue.isEmpty {
        let (currentX, currentY) = queue.removeFirst()
        for (dx, dy) in directions {
            let qx = dx + currentX
            let qy = dy + currentY
            // ์ธ์ ‘ ๋…ธ๋“œ ๋ฒ”์œ„ ์ฒดํฌ & ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ์ฒดํฌ
            if qx >= 0 && qx < n && qy >= 0 && qy < m && visited[qx][qy] < answer {
                if graph[qx][qy] > 0 { // ํ•ด๋‹น ์นธ์ด ๋น™์‚ฐ์ธ ๊ฒฝ์šฐ
                    visited[qx][qy] = answer // ๋ฐฉ๋ฌธ ํ‘œ์‹œ
                    queue.append((qx, qy)) // ํ์— ์ถ”๊ฐ€
                } else { // ํ•ด๋‹น ์นธ์ด ๋ฐ”๋‹ค์ธ ๊ฒฝ์šฐ
                    graph[currentX][currentY] -= 1 // ๋น™์‚ฐ ๋†’์ด ๊ฐ์†Œ์‹œํ‚ด
                }
            }
        }
    }
}

while count == 1 { // ๋น™์‚ฐ์ด ํ•˜๋‚˜์˜ ๋ฉ์–ด๋ฆฌ๋กœ ์ด์–ด์ ธ ์žˆ๋Š” ๋™์•ˆ ๋ฐ˜๋ณต
    count = 0 // count 0์œผ๋กœ ์ดˆ๊ธฐํ™”. bfs ์ˆ˜ํ–‰๋˜๋ฉด(= ๋…น์ผ ๋น™์‚ฐ์ด ์žˆ๋‹ค๋ฉด) 1์ด ๋จ 
    answer += 1
    for i in 0..<n {
        for j in 0..<m {
            // ์ด๋ฒˆ ํ•ด์— ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๋Š” ๋น™์‚ฐ ์ฐพ๊ธฐ
            if arr[i][j] > 0 && visited[i][j] < answer {
                visited[i][j] = answer 
                count += 1 // bfs๊ฐ€ 2๋ฒˆ ์ด์ƒ ํ˜ธ์ถœ๋˜๋ฉด ๋ฉ์–ด๋ฆฌ๊ฐ€ 2๊ฐœ ์ด์ƒ์ด๋ผ๋Š” ๋œป
                bfs(&arr, i, j)
            }
        }
    }
    if count == 0 { // ๋…น์ผ ๋น™์‚ฐ์ด ์—†๋Š” ๊ฒฝ์šฐ(= ๋‹ค 0์ด๋ผ๋Š” ๋œป)
        answer = 1
        break
    }
}

print(answer - 1)