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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 14502๋ฒˆ - ์—ฐ๊ตฌ์†Œ (BFS ๋ฌธ์ œํ’€์ด)

๊ฐ์ž ๐Ÿฅ” 2022. 7. 14. 20:13
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ ๋งํฌ

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

 

14502๋ฒˆ: ์—ฐ๊ตฌ์†Œ

์ธ์ฒด์— ์น˜๋ช…์ ์ธ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์—ฐ๊ตฌํ•˜๋˜ ์—ฐ๊ตฌ์†Œ์—์„œ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์œ ์ถœ๋˜์—ˆ๋‹ค. ๋‹คํ–‰ํžˆ ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์•„์ง ํผ์ง€์ง€ ์•Š์•˜๊ณ , ๋ฐ”์ด๋Ÿฌ์Šค์˜ ํ™•์‚ฐ์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ ์—ฐ๊ตฌ์†Œ์— ๋ฒฝ์„ ์„ธ์šฐ๋ ค๊ณ  ํ•œ๋‹ค. ์—ฐ๊ตฌ์†Œ๋Š” ํฌ

www.acmicpc.net

 

๐ŸŸ  ๋ฌธ์ œ ํ’€์ด ์•„์ด๋””์–ด

0๊ณผ 1๊ณผ 2๋ฅผ ๊ตฌ๋ณ„ํ•ด์•ผํ•œ๋‹ค๋Š” ์ ์ด ์–ด๋ ค์› ๋‹ค. ์—ญ์‹œ ๊ณจ๋“œ๋Š” ๋‚˜์—๊ฒŒ ๋ฌด๋ฆฌ์ธ๊ฒƒ์ธ๊ฐ€ ใ… ใ… ใ…  ์‹ค๋ ฅ์–ธ์ œ๋Š˜์–ด ๋Š˜๊ณ ๋Š” ์žˆ๋‹ˆ?

๊ณ ๋ฏผ๊ณ ๋ฏผ๊ณ ๋ฏผ ํ•˜๋‹ค๊ฐ€ ๋„์ €ํžˆ ๊ฐ์ด ์žกํžˆ์ง€ ์•Š์•„์„œ ์–ด๋–ป๊ฒŒ ํ’€์—ˆ๋‚˜ ์ฐธ๊ณ ๋ฅผ ํ•ด๋ณด์•˜๋Š”๋ฐ ํ’€์ด ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ๊ฐ™์•˜๋‹ค.

  • BFS๋Š” ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ• ๊นŒ
    • ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผ๋œจ๋ฆฌ๋Š”๊ฒƒ์ด ๋ชฉํ‘œ๊ธฐ๋–„๋ฌธ์— ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ queue์— ๋„ฃ์–ด์ฃผ๊ณ , ์‚ฌ๋ฐฉ๋ฉด์— ์œ„์น˜ํ•œ ๊ฐ’์ด 0 ์ด๋ผ๋ฉด, 2๋กœ ์ž…๋ ฅํ•ด์ฃผ๋ฉด์„œ BFS ๋ฅผ ๋Œ๋ ธ๋‹ค. ์ฆ‰, BFS๋ฅผ ๋Œ๋ฆฌ๋ฉด์„œ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์ „ํŒŒํ•ด์ฃผ์—ˆ๋‹ค.
  • ๋ฒฝ์€ ๋”ฑ ์„ธ๊ฐœ ์„ธ์šธ ์ˆ˜ ์žˆ๋Š”๋ฐ,  ์–ด๋–ป๊ฒŒ ์„ธ์šธ๊นŒ 
    • ํŒŒ์ด์ฌ์œผ๋กœ ํ‘ผ ํ’€์ด๋“ค์€ ๋Œ€๋ถ€๋ถ„ ์กฐํ•ฉ..?? ๋ญ ์จ‹๋“  ๊ทธ collection์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋ฒฝ์„ ์„ธ์šธ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•œ๋’ค, ํ•ด๋‹น ์œ„์น˜์— ๋ฒฝ์„ ์„ธ๊ฐœ ์ƒˆ์› ์„๋•Œ์˜ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ–ˆ๋‹ค.
    • ํ•˜์ง€๋งŒ swift๋Š” ์ด๋Ÿฐ๊ธฐ๋Šฅ์ด์—†์–ด์„œ 3์ค‘for๋ฌธ์„ ์ด์šฉํ•ด์„œ 0์ธ ์œ„์น˜๋“ค์„ ์ „๋ถ€ ํƒ์ƒ‰ํ•˜๊ณ , ์ด์ค‘์— ์„ธ๊ฐœ์˜ ์œ„์น˜๋ฅผ ๊ณจ๋ผ์„œ 1์„ ๋„ฃ์–ด์ค€๋’ค์ฐจ๊ทผ์ฐจ๊ทผ bfs๋ฅผ ๋Œ๋ ค์ฃผ์—ˆ๋‹ค ^___^
  • ํ•ต์‹ฌ ํฌ์ธํŠธ๋Š”, 0์ธ ์œ„์น˜๋ฅผ Empty ๋ผ๋Š” ๋ณ€์ˆ˜์— ๋ชจ๋‘ ๋„ฃ์–ด์ค€ ๋’ค, ์œ„์น˜ 3๊ฐœ์˜ ์กฐํ•ฉ์„ ๋ชจ๋‘ ๊ณ ๋ คํ•˜์—ฌ ์ „๋ถ€ ํƒ์ƒ‰ํ•ด์ค€๋‹ค๋Š” ์ ์ด ํฌ์ธํŠธ์˜€๋‹ค.

 

๐ŸŸ  ์ •๋‹ต์ฝ”๋“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Boj/14502_%EC%97%B0%EA%B5%AC%EC%86%8C/main.swift

 

GitHub - deslog/Algorithm

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

github.com

let nm = readLine()!.split(separator: " ").map { Int(String($0))! }
let n = nm[0] // ์„ธ๋กœ
let m = nm[1] // ๊ฐ€๋กœ
var lab = [[Int]]()
var empty = [(Int, Int)]()
var virus = [(Int, Int)]()
var max = 0
let dx = [-1, 1, 0, 0]
let dy = [0, 0, -1, 1]

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

for y in 0..<n {
    for x in 0..<m {
        if lab[y][x] == 0 {
            empty.append((y, x))
        } else if lab[y][x] == 2 {
            virus.append((y, x))
        }
    }
}

// bfs
// ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์ „ํŒŒ์‹œํ‚ค๋ฉด์„œ bfs๋Œ๋ ค์คŒ
func bfs(copyLab: Array<Array<Int>>) {
    var bfsLab = copyLab // lab์„ ๋ฐ”๊ฟ”๋ฒ„๋ฆฌ๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋งˆ์ง€๋ง‰์— bfs๋Œ๋ฆด ๋•Œ copy ํ•ด์„œ ๋Œ๋ฆด ๊ฑฐ์ž„
    var queue = [(Int, Int)]()
    var visited = Array(repeating: Array(repeating: false, count: m), count: n)
    var cnt = 0
    
    for i in 0..<virus.count {
        queue.append(virus[i])
    }

    while !queue.isEmpty {
        let yx = queue.removeFirst()
        let y = yx.0
        let x = yx.1
        
        for i in 0..<4 {
            let ny = y + dy[i]
            let nx = x + dx[i]
            
            // ๋ฒ”์œ„ ํ™•์ธํ•˜๊ณ , 0 ์ด๋ฉด 2๋กœ ๋ฐ”๊พธ๋ฉด์„œ ๋ฐ”์ด๋Ÿฌ์Šค ์ „ํŒŒ์‹œํ‚ด, ๊ทธ๋ฆฌ๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
            if nx >= 0, ny >= 0, nx < m, ny < n {
                if bfsLab[ny][nx] == 0, visited[ny][nx] == false {
                    queue.append((ny, nx))
                    visited[ny][nx] = true
                    bfsLab[ny][nx] = 2
                }
            }
        }
    }
    
    // 0 ์„ธ์–ด์ฃผ๊ธฐ
    for i in 0..<n {
        cnt += bfsLab[i].filter { ($0) == 0 }.count
    }
    
    // max ๊ฐ’ ๊ฐฑ์‹ ํ•ด์ฃผ๊ธฐ
    if max < cnt {
        max = cnt
    }
}

// ์ „์ฒด ๋Œ๋ฉด์„œ 1 ๋„ฃ์–ด์ฃผ๊ธฐ
for i in 0..<empty.count {
    for j in 0..<i {
        for k in 0..<j {
            let y1 = empty[i].0
            let x1 = empty[i].1
            let y2 = empty[j].0
            let x2 = empty[j].1
            let y3 = empty[k].0
            let x3 = empty[k].1
            
            lab[y1][x1] = 1
            lab[y2][x2] = 1
            lab[y3][x3] = 1
            
            let newLab = lab
            bfs(copyLab: newLab)
            
            lab[y1][x1] = 0
            lab[y2][x2] = 0
            lab[y3][x3] = 0
        }
    }
}

print(max)

 

 

 

๐Ÿ“– Reference

์•„๋ž˜ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ–ˆ์Šด๋‹ˆ๋‹ค์•™

https://cotak.tistory.com/14

 

[๋ฐฑ์ค€] 14502 - ์—ฐ๊ตฌ์†Œ [Python(ํŒŒ์ด์ฌ)]

๋ฌธ์ œ www.acmicpc.net/problem/14502 14502๋ฒˆ: ์—ฐ๊ตฌ์†Œ ์ธ์ฒด์— ์น˜๋ช…์ ์ธ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์—ฐ๊ตฌํ•˜๋˜ ์—ฐ๊ตฌ์†Œ์—์„œ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์œ ์ถœ๋˜์—ˆ๋‹ค. ๋‹คํ–‰ํžˆ ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์•„์ง ํผ์ง€์ง€ ์•Š์•˜๊ณ , ๋ฐ”์ด๋Ÿฌ์Šค์˜ ํ™•์‚ฐ์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ

cotak.tistory.com

 

๋ฐ˜์‘ํ˜•