Skip to content

Commit

Permalink
reference/#166: 참고 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-muuu committed Oct 12, 2023
1 parent f01641f commit 365649d
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions AlgorithmReview/Programmers/Lv3/경주로 건설.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,40 @@

import Foundation

func timeout(_ board:[[Int]]) -> Int {
func reference(_ board:[[Int]]) -> Int {

var visit = [[Bool]](repeating: [Bool](repeating: false, count: board.count), count: board.count)
let size = board.count
var board = board
var dp = [[Int]](repeating: [Int](repeating: size*size*500, count: board.count), count: board.count)
var ans = Int.max

func valid(_ i: Int, _ j: Int) -> Bool {
i < board.count && i >= 0 && j < board.count && j >= 0 && board[i][j] == 0 && !visit[i][j]
}

func move(_ i: Int, _ j: Int, _ d: (Int, Int), _ s: Int, _ c: Int) {
func move(_ i: Int, _ j: Int, _ d: (Int, Int), _ cost: Int) {

if i == board.count-1 && j == board.count-1 {
ans = min(ans, 100*s + 500*c); return
} else if 100*s + 500*c >= ans {
if i == size-1 && j == size-1 {
ans = min(ans, cost)
return
} else if dp[i][j] < cost {
return
}

for coord in [(1,0),(-1,0),(0,1),(0,-1)] {
dp[i][j] = cost

for coord in [(0,1),(1,0),(0,-1),(-1,0)] {

This comment has been minimized.

Copy link
@dev-muuu

dev-muuu Oct 12, 2023

Author Owner

좌표 순서에 따라 결과가 달라질 수 있음.. 주의...

let row = coord.0 + i, col = coord.1 + j
if valid(row, col){
visit[row][col] = true
let corner = d == coord ? c : c+1
move(row,col,coord,s+1,corner)
visit[row][col] = false
if row < board.count && row >= 0 && col < board.count && col >= 0 && board[row][col] == 0 {
board[row][col] = -1
if d == coord {
move(row,col,coord,cost+100)
} else {
move(row,col,coord,cost+600)
}
board[row][col] = 0
}
}
}

visit[0][0] = true
move(0,0,(0,0),0,-1)
board[0][0] = -1
move(0,0,(0,0),0)

return ans
return ans - 500
}

0 comments on commit 365649d

Please sign in to comment.