[Swift - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] [์นด์นด์˜ค ์ธํ„ด] ๊ฒฝ์ฃผ๋กœ ๊ฑด์„ค

2021. 5. 31. 18:33ใ†๐Ÿง‘๐Ÿป‍๐Ÿ’ป iOS ๊ฐœ๋ฐœ/Swift - Algorithm - Solutions

๐Ÿ•ต๐Ÿป  ๋ฌธ์ œ ํ•ด์„


๊ท€์—ฌ์šด ์ฃ ๋ฅด๋””๐Ÿฆ–

๊ฑด์„ค ํšŒ์‚ฌ์˜ ํšŒ๊ณ„์‚ฌ์ธ ์ฃ ๋ฅด๋””๋Š” ๊ณ ๊ฐ์‚ฌ๋กœ๋ถ€ํ„ฐ ๊ฒฝ์ฃผ๋กœ ๊ฑด์„ค ๊ฒฌ์  ์˜๋ขฐ๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ๊ณ ๊ฐ์‚ฌ๋กœ ๋ฐ›์€ ๊ฒฝ์ฃผ๋กœ ๊ฑด์„ค ๋ถ€์ง€์˜ ์ •๋ณด๋Š” N X N์˜ ๊ฒฉ์žํ˜•ํƒœ์ด๋ฉฐ ์ค‘๊ฐ„์ค‘๊ฐ„ ๋ฒฝ์ด ์„ธ์›Œ์ ธ ์žˆ๋Š” ๋ถ€๋ถ„์€ 1, ๋น„์–ด์žˆ๋Š” ๋ถ€์ง€๋Š” 0์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. 

๊ฒฝ์ฃผ๋กœ์˜ ์‹œ์ž‘์ ์€ (0, 0) , ๋ ์ ์€ (N - 1, N - 1)์ด๋ผ๊ณ  ํ•  ๋•Œ, ๋ฒฝ์„ ํ”ผํ•ด ์ƒํ•˜์ขŒ์šฐ๋กœ ํ•œ ์นธ์”ฉ ๋„๋กœ๋ฅผ ์—ฐ๊ฒฐ ํ•˜์—ฌ ๊ฒฝ์ฃผ๋กœ๋ฅผ ์™„์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋„๋กœ๋Š” ์ธ์ ‘ํ•˜๊ณ  ์žˆ๋Š” ๋นˆ ๋ถ€์ง€ 2๊ฐœ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ๋งŒ๋“ค๊ณ  ์ฝ”๋„ˆ๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” 500์›, ์ง์„ ๋„๋กœ๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” 100์›์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. 

[์ œํ•œ ์‚ฌํ•ญ]

  • board๋Š” 2์ฐจ์› ์ •์‚ฌ๊ฐ ๋ฐฐ์—ด๋กœ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” 3 ์ด์ƒ 25 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • board ๋ฐฐ์—ด์˜ ๊ฐ ์›์†Œ์˜ ๊ฐ’์€ 0 ๋˜๋Š” 1 ์ž…๋‹ˆ๋‹ค.
    • ๋„๋ฉด์˜ ๊ฐ€์žฅ ์™ผ์ชฝ ์ƒ๋‹จ ์ขŒํ‘œ๋Š” (0, 0)์ด๋ฉฐ, ๊ฐ€์žฅ ์šฐ์ธก ํ•˜๋‹จ ์ขŒํ‘œ๋Š” (N-1, N-1)์ž…๋‹ˆ๋‹ค.
    • ์›์†Œ์˜ ๊ฐ’ 0์€ ์นธ์ด ๋น„์–ด ์žˆ์–ด ๋„๋กœ ์—ฐ๊ฒฐ์ด ๊ฐ€๋Šฅํ•จ์„ 1์€ ์นธ์ด ๋ฒฝ์œผ๋กœ ์ฑ„์›Œ์ ธ ์žˆ์–ด ๋„๋กœ ์—ฐ๊ฒฐ์ด ๋ถˆ๊ฐ€๋Šฅํ•จ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
  • board๋Š” ํ•ญ์ƒ ์ถœ๋ฐœ์ ์—์„œ ๋„์ฐฉ์ ๊นŒ์ง€ ๊ฒฝ์ฃผ๋กœ๋ฅผ ๊ฑด์„คํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
  • ์ถœ๋ฐœ์ ๊ณผ ๋„์ฐฉ์  ์นธ์˜ ์›์†Œ์˜ ๊ฐ’์€ ํ•ญ์ƒ 0์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

์ €๋Š” ๋จผ์ € DFS ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ํ•ด๋‹น ์ขŒํ‘œ๊นŒ์ง€ ์‚ฌ์šฉ๋˜๋Š” ์ตœ์†Œ ๊ธˆ์•ก์„ ์ €์žฅํ•  2์ฐจ์› ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ์ ‘๊ทผํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ฝ”๋„ˆ๋ฅผ ์ฒดํฌํ•ด์ฃผ๊ธฐ ์œ„ํ•ด ํ•ด๋‹น ์ขŒํ‘œ๋กœ ์˜ค๊ธฐ ์ „ ๋ฐฉํ–ฅ์˜ ์ธ๋ฑ์Šค๋ฅผ ์ „๋‹ฌ ์ธ์ž๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์ง์„ ์ผ ๊ฒฝ์šฐ์™€ ๋ฐฉํ–ฅ์„ ๋ฐ”๊ฟ” ์ฝ”๋„ˆ๊ฐ€ ๋งŒ๋“ค์–ด์งˆ ๋•Œ๋ฅผ ๋‚˜๋ˆ„์–ด ์ฒ˜๋ฆฌํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. 

๐Ÿ“ฆ  ๋ฌธ์ œ ํ’€์ด & ์ฝ”๋“œ


func solution(_ board:[[Int]]) -> Int {
    let N = board.count
    let dir = [[-1,0,1,0],[0,1,0,-1]]
    
    // ์ขŒํ‘œ๊นŒ์ง€ ๊ฒฝ์ฃผ๋กœ๋ฅผ ๊ฑด์„คํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์ตœ์†Œ cost ์ €์žฅ, Int.max๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”
    var costBoard = [[Int]](repeating: [Int](repeating: Int.max, count: N), count: N)
    
    func isInside(_ r: Int, _ c: Int) -> Bool {
        if r < 0 || r >= N || c < 0 || c >= N {
            return false
        }
        return true
    }
    
    // dir: ํ˜„์žฌ ์œ„์น˜๋กœ ์˜ค๊ฒŒ๋œ ๋ฐฉํ–ฅ ์ธ๋ฑ์Šค
    func dfs(_ cur: (r: Int, c: Int, cost: Int, dir: Int)) {
        
        // ์ด๋™ํ•œ ๊ณณ์ด ๋ฒฝ์ด๊ฑฐ๋‚˜ ๋ฐฐ์—ด์— ์ €์žฅ๋œ ์ตœ์†Ÿ๊ฐ’ ๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ ๋ฐ”๋กœ ๋ฆฌํ„ด        
        if board[cur.r][cur.c] == 1 || cur.cost > costBoard[cur.r][cur.c] {
            return
        }
        
        // ์ตœ์†Ÿ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ 
        costBoard[cur.r][cur.c] = cur.cost
    
        // ์ƒํ•˜์ขŒ์šฐ๋กœ ํƒ์ƒ‰
        for idx in 0..<4 {
            let nr = cur.r + dir[0][idx]
            let nc = cur.c + dir[1][idx]
            
            if isInside(nr, nc) {
                // ์ง์ „ ๋ฐฉํ–ฅ๊ณผ ๊ฐ™์„ ๊ฒฝ์šฐ, cost + 100
                if cur.dir == idx {
                    dfs((r: nr, c: nc, cost: cur.cost + 100, idx))
                } else {
                // ๋ฐฉํ–ฅ์„ ๊บพ์„ ๊ฒฝ์šฐ ์ฝ”๋„ˆ + ์ง์„ ๋„๋กœ๊ฐ€ ์ƒ๊ธฐ๋ฏ€๋กœ cost + (100 + 500)
                    dfs((r: nr, c: nc, cost: cur.cost + 600, idx))
                }
            }
        }
    }
    
    // ์ถœ๋ฐœ์  cost 0์œผ๋กœ ๊ฐฑ์‹ 
    
    costBoard[0][0] = 0
    
    // ์ถœ๋ฐœ์ ์—์„  ์ƒํ•˜์ขŒ์šฐ ์ค‘ ํ•˜, ์šฐ๋ฐฉํ–ฅ๋งŒ ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ๋‘ ๋ฐฉํ–ฅ์œผ๋กœ dfs ํ•จ์ˆ˜ ํ˜ธ์ถœ
    
    dfs((r: 0, c: 1, cost: 100, dir: 1))
    dfs((r: 1, c: 0, cost: 100, dir: 2))
    
    // ๋„์ฐฉ์  ์ตœ์†Ÿ๊ฐ’ ๋ฆฌํ„ด
    return costBoard[N - 1][N - 1]
}

https://programmers.co.kr/learn/courses/30/lessons/67259

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ฒฝ์ฃผ๋กœ ๊ฑด์„ค

[[0,0,0,0,0,0,0,1],[0,0,0,0,0,0,0,0],[0,0,0,0,0,1,0,0],[0,0,0,0,1,0,0,0],[0,0,0,1,0,0,0,1],[0,0,1,0,0,0,1,0],[0,1,0,0,0,1,0,0],[1,0,0,0,0,0,0,0]] 3800 [[0,0,1,0],[0,0,0,0],[0,1,0,1],[1,0,0,0]] 2100 [[0,0,0,0,0,0],[0,1,1,1,1,0],[0,0,1,0,0,0],[1,0,0,1,0,1],[

programmers.co.kr