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

[Swift] 2022 KAKAO BLIND RECRUITMENT ์‚ฌ๋ผ์ง€๋Š” ๋ฐœํŒ

by Fomagran ๐Ÿ’ป 2022. 1. 20.
728x90
๋ฐ˜์‘ํ˜•

Problem

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์‚ฌ๋ผ์ง€๋Š” ๋ฐœํŒ

[[1, 1, 1], [1, 1, 1], [1, 1, 1]] [1, 0] [1, 2] 5 [[1, 1, 1], [1, 0, 1], [1, 1, 1]] [1, 0] [1, 2] 4

programmers.co.kr


Solution

 

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์™„์ „ ํƒ์ƒ‰์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

 

1. ํ”Œ๋ ˆ์ด์–ด์˜ ์œ„์น˜์™€ ๊ฒŒ์ž„ ๋๋‚ฌ์„ ๋•Œ ์›€์ง์ธ ํšŸ์ˆ˜์™€ ์Šน์ž๋ฅผ ์•Œ๋ ค์ค„ ๊ตฌ์กฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค.

 

struct Location {
    var x:Int,y:Int
}

struct GameResult {
    var count:Int,isWinnerA:Bool
}

 

2. board์˜ ๊ฐ€์žฅ ์ž๋ฆฌ๋ฅผ 0์œผ๋กœ ๊ฐ์‹ธ์ค€๋‹ค.

 

Index out of range ์˜ค๋ฅ˜๋ฅผ ์‰ฝ๊ฒŒ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ž…๋‹ˆ๋‹ค.

 

func wrapBoardEdges(_ board:[[Int]]) -> [[Int]] {
    var wrapBoard = board
    for i in 0..<board.count {
        wrapBoard[i].insert(0, at: 0)
        wrapBoard[i].append(0)
    }
    wrapBoard.insert(Array(repeating: 0, count: board[0].count+2), at: 0)
    wrapBoard.append(Array(repeating: 0, count: board[0].count+2))
    return wrapBoard
}

 

3. ์™ผ์ชฝ,์˜ค๋ฅธ์ชฝ,์œ„์ชฝ,์•„๋ž˜์ชฝ ๋„ค ๋ฐฉํ–ฅ์œผ๋กœ ์›€์ง์ผ ๋•Œ์˜ ์œ„์น˜๋ฅผ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด์ค„ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

 

func makeLRUD(_ loc:Location) -> [Location] {
    return [Location(x: loc.x-1, y: loc.y),Location(x: loc.x+1, y: loc.y),Location(x: loc.x, y: loc.y-1),Location(x: loc.x, y: loc.y+1)]
}

 

4. ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ๋ฒˆ๊ฐˆ์•„๊ฐ€๋ฉด์„œ ์›€์ง์ผ move ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

 

๋ฌด์กฐ๊ฑด ์ด๊ธฐ๋Š” ํ”Œ๋ ˆ์ด์–ด์™€ ์ง€๋Š” ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ ์ด๊ธฐ๋Š” ๊ฒฝ์šฐ์™€ ์ง€๋Š” ๊ฒฝ์šฐ์— ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ ๋‹ฌ๋ฆฌํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ์š”.

 

๋งŒ์•ฝ ๋ฌด์กฐ๊ฑด ์ง€๋Š” ํ”Œ๋ ˆ์ด์–ด๋ผ๋ฉด ์ตœ๋Œ€ํ•œ์œผ๋กœ ๋งŽ์ด ์›€์ง์—ฌ์•ผ ํ•˜๋ฏ€๋กœ maxCount๋ฅผ ์ตœ๋Œ“๊ฐ’์„ ์—…๋ฐ์ดํŠธ ํ•ด์ฃผ๊ณ  ์ด๊ธฐ๋Š” ํ”Œ๋ ˆ์ด์–ด๋ผ๋ฉด

 

์ตœ์†Œํ•œ์œผ๋กœ ์›€์ง์—ฌ์•ผ ํ•˜๋ฏ€๋กœ minCount๋ฅผ ์ตœ์†Ÿ๊ฐ’์œผ๋กœ ์—…๋ฐ์ดํŠธ ํ•ด์ค๋‹ˆ๋‹ค.

 

func move(_ board:[[Int]], _ count:Int,_ aLoc:Location,_ bLoc:Location) -> GameResult {
    let isTurnA = count%2 == 0 ? true : false
    let loc = isTurnA ? aLoc : bLoc
    var minCount = Int.max
    var maxCount = 0
    
    if board[loc.x][loc.y] == 0 {
        return GameResult(count: count, isWinnerA: isTurnA)
    }
        
    for l in makeLRUD(loc).filter({board[$0.x][$0.y] != 0}) {
        var newBoard = board
        newBoard[loc.x][loc.y] = 0
        let moved = isTurnA ? move(newBoard,count+1,l,bLoc) : move(newBoard,count+1,aLoc,l)
        let result = isTurnA ? moved.isWinnerA :!moved.isWinnerA
        if result {
            maxCount = max(maxCount,moved.count)
        }else {
            minCount = min(minCount,moved.count)
        }
    }
    
    if minCount == Int.max && maxCount == 0 {
        return GameResult(count: count, isWinnerA: isTurnA)
    }
    
    if minCount != Int.max {
        return GameResult(count: minCount, isWinnerA: !isTurnA)
    }
    
    return GameResult(count: maxCount, isWinnerA: isTurnA)
}

 

5. ์ตœ์ ์˜ ํ”Œ๋ ˆ์ด ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

func solution(_ board:[[Int]], _ aloc:[Int], _ bloc:[Int]) -> Int {
    let wrapBoard = wrapBoardEdges(board)
    let aLoc = Location(x: aloc[0]+1, y: aloc[1]+1)
    let bLoc = Location(x: bloc[0]+1, y: bloc[1]+1)
    let result = move(wrapBoard, 0, aLoc, bLoc)
    return result.count
}

Source Code

 

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€