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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 7576๋ฒˆ - ํ† ๋งˆํ†  (BFS)

๊ฐ์ž ๐Ÿฅ” 2022. 6. 23. 12:07
๋ฐ˜์‘ํ˜•

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

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

 

7576๋ฒˆ: ํ† ๋งˆํ† 

์ฒซ ์ค„์—๋Š” ์ƒ์ž์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ์ •์ˆ˜ M,N์ด ์ฃผ์–ด์ง„๋‹ค. M์€ ์ƒ์ž์˜ ๊ฐ€๋กœ ์นธ์˜ ์ˆ˜, N์€ ์ƒ์ž์˜ ์„ธ๋กœ ์นธ์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋‹จ, 2 ≤ M,N ≤ 1,000 ์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ๋Š” ํ•˜๋‚˜์˜ ์ƒ์ž์— ์ €์žฅ๋œ ํ† ๋งˆํ† 

www.acmicpc.net

 

๐Ÿ’ป ํ’€์ด

BFS๋กœ ํ’€์—ˆ๋‹ค. ์‚ฌ์‹ค ์‹œ์ž‘์ ์ด ์—ฌ๋Ÿฌ๊ฐœ์ธ BFS๋Š” ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผํ• ์ง€ ๋ชจ๋ฅด๊ฒ ์–ด์„œ, ์ธํ„ฐ๋„ท์„ ์ฐธ๊ณ ํ•ด์„œ ํ’€์—ˆ๋‹คใ… ใ…  

  1. m๊ณผ n์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. 
  2. 0, -1, 1์ด ๋“ค์–ด์žˆ๋Š” ๋ฐฐ์—ด์„ box๋กœ ์ž…๋ ฅ๋ฐ›์•˜๋‹ค.
  3. day๋Š” ๋ฉฐ์น ์ด ์ง€๋‚ฌ๋Š”์ง€ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋งŒ๋“ ๋ฐฐ์—ด์ด๋‹ค. ๋’ค์—์„œ ๋ณด๋ฉด ํ•œ์นธํ•œ์นธ ์ต์–ด๋‚˜๊ฐˆ๋•Œ๋งˆ๋‹ค +1 ์„ ํ•ด์ค€๋‹ค.
let mn = readLine()!.split(separator: " ").map { Int(String($0))! }
let m = mn[0]
let n = mn[1]

var box = [[Int]]()
for _ in 0..<mn[1] {
    let temp = readLine()!.split(separator: " ").map{Int(String($0))!}
    box.append(temp)
}

var day = Array(repeating: Array(repeating: 0, count: 1001), count: 1001)

 

  1. dx, dy ์ƒํ•˜์ขŒ์šฐ๋กœ ๋ฐฉํ–ฅํ‚ค๋ฅผ ์„ค์ •ํ–ˆ๋‹ค.
  2. queue๋ฅผ ์‚ฌ์šฉํ•ด์„œ bfs๋กœ ๋Œ๋ ค์ค„ ๊ฒƒ์ด๋‹ค.
  3. lastIdx๋Š” ์ž…๋ ฅ๋ฐ›๋Š” ์ด์œ ๊ฐ€, ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋ฐฉ๋ฌธํ•œ idx์ธ๊ฒฝ์šฐ, ๋งˆ์ง€๋ง‰ ๋‚  ๋ฐฉ๋ฌธํ•œ ์ธ๋ฑ์Šค๋ผ๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ, ์ €์žฅํ•ด๋‘์—ˆ๋‹ค๊ฐ€ ๋งˆ์ง€๋ง‰์— ์ถœ๋ ฅํ•  ๋•Œ ์‚ฌ์šฉํ•ด์ค„ ๊ฒƒ์ด๋‹ค.
  4. idx๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค. ์›๋ž˜ removeFirst๋ฅผ ์‚ฌ์šฉํ•ด์„œ queue์— popํ•ด์„œ ๋„ฃ์–ด์ฃผ๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ํ•˜๊ฒŒ๋˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚  ํ™•๋ฅ ์ด ์ปค์„œ idx๋ฅผ ์‚ฌ์šฉํ•ด๋ณธ๋‹ค.
  5. isCooked ์ต์€ ํ† ๋งˆํ† ๊ฐ€ ์žˆ๋ƒ ์—†๋ƒ๋ฅผ ๋”ฐ์งˆ๋•Œ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค.
let dx = [0, 0, -1, 1]
let dy = [-1, 1, 0, 0]

var queue = [[Int]]()
var lastIdx = [0, 0]
var idx = 0
var isCooked = true

 

 

  1. ๋จผ์ € queue์—๋‹ค๊ฐ€ ์ต์€ ํ† ๋งˆํ† ๋“ค์„ ๋„ฃ์–ด์ค€๋‹ค. ์ด ๊ณผ์ •์ด ์กฐ๊ธˆ ํ—ท๊ฐˆ๋ ธ๋Š”๋ฐ, ์‹œ์ž‘์ ์ด ์—ฌ๋Ÿฌ๊ฐœ์ด๋ฏ€๋กœ ์—ฌ๋Ÿฌ๊ฐœ ๋„ฃ๊ณ  ์‹œ์ž‘ํ•ด์ฃผ๋ฉด ๋œ๋‹ค๊ณ  ์ดํ•ดํ•˜์ž.
for i in 0..<n {
    for j in 0..<m {
        if box[i][j] == 1 {
            queue.append([i, j])
        }
    }
}

 

  1. bfsํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค.
  2. bfsํ•จ์ˆ˜์—์„œ๋Š” queue์˜ ์ˆ˜๋ณด๋‹ค, ์ธ๋ฑ์Šค ์ˆ˜๊ฐ€ ๋” ์ž‘์„๋•Œ๊นŒ์ง€ ๋Œ๋ฆฐ๋‹ค.
func bfs() {
    while idx < queue.count {
        let pop = queue[idx]
        idx += 1

        for k in 0..<4 {
            let nx = pop[0] + dx[k]
            let ny = pop[1] + dy[k]
            
            if nx >= 0 && nx < n && ny >= 0 && ny < m {
                if box[nx][ny] == 0 {
                    box[nx][ny] = 1
                    queue.append([nx, ny])
                    day[nx][ny] = day[pop[0]][pop[1]] + 1
                    lastIdx = [nx, ny]
                }
            }
        }
    }
}

bfs()

 

  1. ์—ฌ๊ธฐ์„œ์ค‘์š”ํ–ˆ๋˜ ๊ฐœ๋…, ์ต์€ ํ† ๋งˆํ† ๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์žˆ์œผ๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ต์—ˆ๋Š”์ง€ ํŒ๋ณ„ํ•œ ํ›„์— ์ถœ๋ ฅํ•ด์ค€๋‹ค.
for i in 0..<n {
    for j in 0..<m {
        if box[i][j] == 0 {
            isCooked = false
        }
    }
}

if isCooked {
    print(day[lastIdx[0]][lastIdx[1]])
} else {
    print(-1)
}

 

๐Ÿ’ป ์†Œ์Šค์ฝ”๋“œ 

https://github.com/deslog/Algorithm/blob/main/Algorithm/Boj/7576_%ED%86%A0%EB%A7%88%ED%86%A0/main.swift

 

GitHub - deslog/Algorithm

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

github.com

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

for _ in 0..<mn[1] {
    let temp = readLine()!.split(separator: " ").map{Int(String($0))!}
    box.append(temp)
}

let dx = [0, 0, -1, 1]
let dy = [-1, 1, 0, 0]

var queue = [[Int]]()
var lastIdx = [0, 0]
var idx = 0
var isCooked = true

for i in 0..<n {
    for j in 0..<m {
        if box[i][j] == 1 {
            queue.append([i, j])
        }
    }
}

func bfs() {
    while idx < queue.count {
        let pop = queue[idx]
        idx += 1

        for k in 0..<4 {
            let nx = pop[0] + dx[k]
            let ny = pop[1] + dy[k]
            
            if nx >= 0 && nx < n && ny >= 0 && ny < m {
                if box[nx][ny] == 0 {
                    box[nx][ny] = 1
                    queue.append([nx, ny])
                    day[nx][ny] = day[pop[0]][pop[1]] + 1
                    lastIdx = [nx, ny]
                }
            }
        }
    }
}

bfs()
for i in 0..<n {
    for j in 0..<m {
        if box[i][j] == 0 {
            isCooked = false
        }
    }
}

if isCooked {
    print(day[lastIdx[0]][lastIdx[1]])
} else {
    print(-1)
}
๋ฐ˜์‘ํ˜•