๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

[Swift] 2020 KAKAO INTERNSHIP ๊ฒฝ์ฃผ๋กœ ๊ฑด์„ค

by Fomagran ๐Ÿ’ป 2021. 8. 31.
728x90
๋ฐ˜์‘ํ˜•

Problem

 

 

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

[[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


Solution

 

ํ•ด๋‹น ๋ฌธ์ œ๋Š” DFS๋กœ ํ’€์–ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

 

1. ์ž๋™์ฐจ์™€ ์ƒํ•˜์ขŒ์šฐ ๋ฐฉํ–ฅ ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด์ค€๋‹ค. (ํ•„์ˆ˜๋Š” ์•„๋‹˜)

 

์ž๋™์ฐจ์— ํ•„์š”ํ•œ x,y,price,prev์™€ ์ƒํ•˜์ขŒ์šฐ ๋ฐฉํ–ฅ์„ enum์œผ๋กœ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด๋†”์„œ ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

struct Car {
    var x:Int,y:Int,price:Int,prev:Direction
}

enum Direction {
    case left,right,up,down
}

 

2. ์–ด๋–ค ๊ณณ์„ ๋“ค๋ ธ๋Š”์ง€ ํ™•์ธํ•ด์ค„ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

 var visited:[[Int]] = Array(repeating: Array(repeating:Int.max, count: board.count), count: board.count)

 

3. ์ฒ˜์Œ ์‹œ์ž‘ํ•˜๋Š” ๋ฐฉํ–ฅ ์˜ค๋ฅธ์ชฝ๊ณผ ์•„๋ž˜์ชฝ์œผ๋กœ ์žฌ๊ท€๋ฅผ ์‹œ์ž‘ํ•ด์ค๋‹ˆ๋‹ค.

 

์ฒ˜์Œ์—” ์™ผ์ชฝ๊ณผ ์œ„์ชฝ์œผ๋กœ ์›€์ง์ผ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋‘๊ฐ€์ง€ ๋ฐฉํ–ฅ์œผ๋กœ ์žฌ๊ท€๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค.

 

๋˜ํ•œ ๋ฌด์กฐ๊ฑด ์ง์„  ๋ฐฉํ–ฅ์œผ๋กœ ์›€์ง์ผ ์ˆ˜ ๋ฐ–์— ์—†์œผ๋ฏ€๋กœ ๊ฐ€๊ฒฉ์€ 100์œผ๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

 

 moveFourDirection(car: Car(x: 1, y: 0, price: 100, prev:.right),board: board,visited: &visited)
 moveFourDirection(car: Car(x: 0, y: 1, price: 100, prev:.down),board:board,visited: &visited)

 

4.  ํ˜„์žฌ ์œ„์น˜๊ฐ€ ๊ฐ€๋Šฅํ•œ ์œ„์น˜์ธ์ง€ ํ™•์ธํ•ด์ค๋‹ˆ๋‹ค.

 

๋จผ์ € x,y ๋ชจ๋‘ board์•ˆ์— ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์ฃผ๊ณ  ํ˜„์žฌ ์œ„์น˜๊ฐ€ ๋ฒฝ์ธ์ง€ ์•„๋‹Œ์ง€ ํ™•์ธํ•ด์ค๋‹ˆ๋‹ค.

 

๋˜ํ•œ visited์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฐ€๊ฒฉ์ด ํ˜„์žฌ ์œ„์น˜์˜ ๊ฐ€๊ฒฉ๋ณด๋‹ค ์ ๊ฑฐ๋‚˜ ๊ฐ™์„ ๋•Œ๋งŒ ์›€์ง์—ฌ์ค๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ํ˜„์žฌ ์œ„์น˜๊ฐ€ ์ด๋ฏธ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฐ€๊ฒฉ๋ณด๋‹ค ๋” ๋น„์‹ธ๋‹ค๋ฉด ์˜๋ฏธ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋ฉˆ์ถฐ์ค๋‹ˆ๋‹ค.

 

func isEnabled(car:Car,board:[[Int]],visited:[[Int]]) -> Bool {
    return 0..<board.count ~= car.x && 0..<board.count ~= car.y && car.price <= visited[car.y][car.x] && board[car.y][car.x] == 0
}

 

5. 4๊ฐ€์ง€ ๋ฐฉํ–ฅ์œผ๋กœ ์žฌ๊ท€ํ•ด์ค๋‹ˆ๋‹ค.

 

๋จผ์ € visited์— ํ˜„์žฌ ๊ฐ€๊ฒฉ์„ ์„ค์ •ํ•ด์ฃผ๊ณ  ์ƒˆ๋กœ์šด ์ž๋™์ฐจ๋Š” ๋ฐฉํ–ฅ์— ๋”ฐ๋ฅธ x์™€ y๊ฐ’์„ ๋”ํ•ด์ค๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ car์˜ ์ด์ „ ๋ฐฉํ–ฅ์ด ํ˜„์žฌ ์ด๋™ํ•˜๋ ค๋Š” ๋ฐฉํ–ฅ๊ณผ ๋˜‘๊ฐ™๋‹ค๋ฉด ์ง์„ ์ด๋ฏ€๋กœ 100์„ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ  

 

์•„๋‹ˆ๋ผ๋ฉด 600์„ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณค ์ƒˆ๋กœ์šด visited๋ฅผ ๋„ฃ๊ณ  ์žฌ๊ท€ํ•ด์ค๋‹ˆ๋‹ค.

 

func moveFourDirection(car:Car,board:[[Int]],visited:inout [[Int]]) {
    let direction:[Direction:(Int,Int)] = [.right:(1,0),.left:(-1,0),.down:(0,1),.up:(0,-1)]
    if isEnabled(car: car, board: board, visited: visited) {
        visited[car.y][car.x] = car.price
        for (key,value) in direction {
            var newCar = Car(x: car.x + value.0, y: car.y+value.1, price: car.price, prev: key)
            newCar.price += car.prev == key ? 100 : 600
            moveFourDirection(car: newCar, board: board, visited: &visited)
        }
    }
}

 

6. visited์˜ ๊ฐ€์žฅ ๋์— ์žˆ๋Š” ๋„์ฐฉ์ ์˜ ๊ฐ€๊ฒฉ์„ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

 

return visited.last!.last!

Source Code

 


P.S

 

์ฒ˜์Œ์—” BFS๋กœ ํ’€์–ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ธ์ง€ ์•Œ๊ณ  BFS๋กœ ํ’€์—ˆ๋”๋‹ˆ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค 25๋ฒˆ ๋นผ๊ณ  ๋‹ค ๋งž์•˜๋‹ค..

 

์ด๊ฒŒ ํ•จ์ •์ด์—ˆ์Œ.. ๊ทธ๋ž˜์„œ ๋‹น์—ฐํžˆ BFS๋กœ ํ’€์–ด์•ผํ•˜๊ณ  ์—ฃ์ง€ ์ผ€์ด์Šค๊ฐ€ ์žˆ๊ตฌ๋‚˜ ์‹ถ์–ด์„œ ๊ณ„์† ์—ฃ์ง€ ์ผ€์ด์Šค๋ฅผ ์ฐพ๋‹ค๊ฐ€ 

 

๊ฒฐ๊ตญ ์•„ DFS๋กœ ํ’€์–ด์•ผํ•˜๋Š” ๋ฌธ์ œ์ด๊ตฌ๋‚˜๋ฅผ ๊นจ๋‹ซ๊ณ  DFS๋กœ ๋ฐ”๊ฟจ๋”๋‹ˆ ๋ฐ”๋กœ ํ†ต๊ณผ๊ฐ€ ๋˜์—ˆ๋‹ค...

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€