Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 2636๋ฒˆ - ์น˜์ฆˆ (BFS ํ’€์ด)

๊ฐ์ž ๐Ÿฅ” 2022. 7. 6. 21:51
๋ฐ˜์‘ํ˜•

โœ… ๋ฌธ์ œ ๋งํฌ

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

 

2636๋ฒˆ: ์น˜์ฆˆ

์•„๋ž˜ <๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ์ •์‚ฌ๊ฐํ˜• ์นธ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์‚ฌ๊ฐํ˜• ๋ชจ์–‘์˜ ํŒ์ด ์žˆ๊ณ , ๊ทธ ์œ„์— ์–‡์€ ์น˜์ฆˆ(ํšŒ์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ ๋ถ€๋ถ„)๊ฐ€ ๋†“์—ฌ ์žˆ๋‹ค. ํŒ์˜ ๊ฐ€์žฅ์ž๋ฆฌ(<๊ทธ๋ฆผ 1>์—์„œ ๋„ค๋ชจ ์นธ์— X์นœ ๋ถ€๋ถ„)์—๋Š” ์น˜์ฆˆ๊ฐ€ ๋†“

www.acmicpc.net

 

โœ… ๋ฌธ์ œ ํ’€์ด ์•„์ด๋””์–ด

๋ฌธ์ œ๊ฐ€ ๊ต‰์žฅํžˆ ๋‚œํ•ดํ•˜๊ฒŒ ์ƒ๊ฒผ๊ธฐ ๋•Œ๋ฌธ์— ,,,, ์ฒ˜์Œ์—” BFS ๋ฌธ์ œ์ธ์ง€ ๊ฐ์ด ์•ˆ์žกํ˜”๋‹ค. ์ธํ„ฐ๋„ท์„ ์ฐพ์•„๋ณด๋‹ˆ, ์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์•˜๋‹ค.

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

๊ทธ๋ž˜์„œ (0,0)๋ถ€ํ„ฐ ํ•ญ์ƒ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•˜๊ณ , ์น˜์ฆˆ๊ฐ€ ์žˆ๋Š” ์ž๋ฆฌ (1) ์„ ๋งŒ๋‚˜๋ฉด ํ•ด๋‹น ์ž๋ฆฌ๋ฅผ 0์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ , ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅด ํ•ด์ค€๋‹ค. (์ด ์ž๋ฆฌ๋Š” queue์— ๋„ฃ์–ด์ฃผ์ง€ ์•Š๋Š”๋‹ค.) ๊ทธ๋ฆฌ๊ณ  ์น˜์ฆˆ๋ฅผ ๋…น์˜€๋‹ค๋ฉด, cnt์— +1 ํ•ด์คŒ์œผ๋กœ์จ ๋…น์ธ์น˜์ฆˆ์˜ ๊ฐฏ์ˆ˜๋ฅผ ์„ธ์–ด์ค„๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ ๋งˆ์ฃผ์นœ ์ž๋ฆฌ๊ฐ€ ๊ณ„์†ํ•ด์„œ ์™ธ๋ถ€๊ณต๊ธฐ (0)์ด๋ผ๋ฉด, ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๊ณ  queue์— ๋„ฃ๋Š” ๋ฐฉ์‹์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

๋…น์ธ ์น˜์ฆˆ์˜ ๊ฐฏ์ˆ˜๋Š” cheeseCnt ๋ฐฐ์—ด์— ๋„ฃ์–ด์ค„ ๊ฒƒ์ด๋‹ค. ๋งˆ์ง€๋ง‰์— '๋‹ค๋…น๊ธฐ ์ „์˜ ์น˜์ฆˆ์˜ ์ˆ˜'๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ์–ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐฐ์—ด๋กœ ๊ฐ๊ฐ์˜ ๋‹จ๊ณ„๋งˆ๋‹ค ๋…น์ธ ์น˜์ฆˆ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ , ๋งจ ๋’ค์—์„œ ํ•˜๋‚˜ ์•ž์—์žˆ๋Š” ๊ฐ’์„ ์ถœ๋ ฅํ•ด์ค„ ๊ฒƒ์ด๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋…น์ธ ์‹œ๊ฐ„ time ์€ bfs๊ฐ€ ํ•œ๋ฒˆ๋Œ์•„๊ฐˆ ๋•Œ๋งˆ๋‹ค +1์„ ํ•ด์ฃผ์–ด ์ถœ๋ ฅํ•ด์ค„ ๊ฒƒ์ด๋‹ค.

 

โœ… ์ „์ฒด ์†Œ์Šค์ฝ”๋“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Boj/2636_%EC%B9%98%EC%A6%88/main.swift

 

GitHub - deslog/Algorithm

Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

//
//  main.swift
//  Algorithm
//
//  Created by LeeJiSoo on 2022/07/06.
//
// ref: https://hooongs.tistory.com/264
let dx = [-1, 1, 0, 0]
let dy = [0, 0, -1, 1]
let nm = readLine()!.split(separator: " ").map { Int(String($0))! }
let n = nm[0]
let m = nm[1]
var cheeseCnt = [Int]()
var cheeseBoard = [[Int]]()
var time = 0
var cnt = 0

for _ in 0..<n {
    cheeseBoard.append(readLine()!.split(separator: " ").map{ Int(String($0))! })
}

func bfs() -> Int {
    var visited = Array(repeating: Array(repeating: false, count: m), count: n) // bfs๋Œ๋–„๋งˆ๋‹ค 0,0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋‹ˆ๊นŒ visited ๋„ ์ดˆ๊ธฐํ™”
    var queue = [(Int, Int)]() // bfs๋Œ queue ์ดˆ๊ธฐํ™”
    queue.append((0, 0))
    visited[0][0] = true
    cnt = 0  // ํ•œ๋ฒˆ ๋Œ ๋•Œ ๋ช‡๊ฐœ์˜ ์น˜์ฆˆ๊ฐ€ ์‚ฌ๋ผ์ง€๋Š”์ง€ count -> ๋‚˜์ค‘์— cheesecnt์— ๋„ฃ์–ด์ค„๊ฑฐ์ž„
    
    while !queue.isEmpty {
        let xy = queue.removeFirst()
        let x = xy.0
        let y = xy.1
        
        for i in 0..<4 {
            let nx = x + dx[i]
            let ny = y + dy[i]
            
            if nx >= 0, nx < n, ny >= 0, ny < m, !visited[nx][ny] {
                if cheeseBoard[nx][ny] == 0 {  // ๊ณต๊ธฐ์ž๋ฆฌ ์ผ ๋•Œ
                    visited[nx][ny] = true
                    queue.append((nx, ny))
                } else if cheeseBoard[nx][ny] == 1 { // ์น˜์ฆˆ๊ฐ€ ์žˆ๋Š” ์œ„์น˜์ผ ๋•Œ
                    visited[nx][ny] = true
                    cheeseBoard[nx][ny] = 0
                    cnt += 1
                }
            }
        }
    }
    cheeseCnt.append(cnt)
    return cnt
}

while true {
    time += 1
    cnt = bfs()
    if cnt == 0 { // ๋”์ด์ƒ ๋…น์„ ์น˜์ฆˆ๊ฐ€ ์—†๋‹ค๋Š” ๋œป
        break
    }
}

print(time-1)
print(cheeseCnt[cheeseCnt.count-2])
๋ฐ˜์‘ํ˜•