๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail
 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

๋ฌธ์ œ ์„ค๋ช…

 

 

๋ฌธ์ œ ํ’€์ด

 

์ฒจ์— ๋ฌด์‹ํ•˜๊ฒŒ DFS๋กœ ํ™”์‚ด์˜ ํ•ฉ์ด n์ด ๋˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๊ตฌํ–ˆ๋‹ค.

 

ex) n์ด 3์ด๋ฉด

[3,0,0,0,0,0,0,0,0,0,0]

[2,1,0,0,0,0,0,0,0,0,0]

[0,3,0,0,0,0,0,0,0,0,0]

[0,2,1,0,0,0,0,0,0,0,0]

...

 

n์ด 9๋ฅผ ๋„˜์–ด๊ฐ€๋‹ˆ๊นŒ ์‹œ๊ฐ„์ดˆ๊ณผ..

 

๊ฒฐ๊ตญ ์ •๋‹ต์„ ์‚ด์ง ๋ณด๊ณ  ๋‹ค์‹œ ๊ตฌํ˜„ํ•ด ๋ณด์•˜๋‹ค.

 

ํ•ต์‹ฌ์€ 

์–ดํ”ผ์น˜๋ฅผ ์ด๊ธฐ๋Š” ๊ฒฝ์šฐ / ์ง€๋Š” ๊ฒฝ์šฐ๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ

 

์–ดํ”ผ์น˜์˜ ํ™”์‚ด์ด

[2,3,1,0,0,0,0,1,3,0,0]

์ผ ๋•Œ

 

์–ดํ”ผ์น˜๋ฅผ ์ด๊ธฐ๋Š” ๊ฒฝ์šฐ

0๋ฒˆ์งธ ์ธ๋ฑ์Šค(10์ )์„ ์ด๊ธฐ๋Š” ๊ฒฝ์šฐ๋Š” ๋ผ์ด์–ธ์ด 3๋ฐœ์„ ์˜์•˜์„ ๋•Œ๋‹ค. (์—ฌ๊ธฐ์„œ 4๋ฐœ ์ด์ƒ ์˜๋ฉด ํ™”์‚ด ๋‚ญ๋น„๋กœ ์˜๋ฏธ๊ฐ€ ์—†๋‹ค.)

 

์–ดํ”ผ์น˜๋ฅผ ์ง€๋Š” ๊ฒฝ์šฐ

์–ด์ฐจํ”ผ ์งˆ ๊ฑฐ ํ™”์‚ด ๋‚ญ๋น„ ์—†์ด 0๊ฐœ๋กœ ์ง„๋‹ค.

 

 

์ด ๊ฒฝ์šฐ๋กœ DFS๋ฅผ ๋Œ๋ ค์ค€๋‹ค.

 

DFS(ํ˜„์žฌ์ธ๋ฑ์Šค(=๊นŠ์ด), ํ™”์‚ด์˜ ์ดํ•ฉ, ํ™”์‚ด ๋ฆฌ์ŠคํŠธ)

๋กœ ๋Œ๋ฆฌ๋ฉฐ

 

๊นŠ์ด๊ฐ€ 10์ด ๋˜์—ˆ๊ฑฐ๋‚˜ ํ™”์‚ด์„ ๋‹ค ์ผ์„ ๋•Œ DFS๋ฅผ ์ข…๋ฃŒ์‹œํ‚จ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋งŒ์•ฝ์— ํ™”์‚ด์ด ๋‚จ์•˜๋Š”๋ฐ ์ข…๋ฃŒ ๋˜์—ˆ์œผ๋ฉด ๋‚จ์€ ํ™”์‚ด์„ 0์ ์— ๋ชฐ์•„๋„ฃ๋Š”๋‹ค. (์ ์ˆ˜์ฐจ๊ฐ€ ๊ฐ™์„ ๊ฒฝ์šฐ ๋‚ฎ์€์ ์ˆ˜์— ํ™”์‚ด์ด ๋งŽ์€๊ฒŒ ์œ ๋ฆฌํ•˜๋ฏ€๋กœ)

 

 

์ฝ”๋“œ

func solution(_ n:Int, _ info:[Int]) -> [Int] {
    
    // ์–ดํ”ผ์น˜์™€ ๋‚˜์˜ ์ ์ˆ˜ ์ฐจ์ด๋ฅผ ๋ฆฌํ„ด
    func getScoreDiff(_ arr: [Int]) -> Int {
        var apeachScore = 0
        var lionScore = 0
        for i in 0..<11 {
            if arr[i] == 0 && info[i] == 0 {
                continue
            }
            if arr[i] > info[i] {
                lionScore += (10-i)
            }
            else {
                apeachScore += (10-i)
            }
        }
        return lionScore - apeachScore
    }
    
    // ๋” ๋‚ฎ์€ ์ ์ˆ˜์˜ ํ™”์‚ด์ด ์žˆ์œผ๋ฉด true ๋ฆฌํ„ด
    func isMoreThanLowScore(answer: [Int], arr: [Int]) -> Bool {
        for i in stride(from: 10, through: 0, by: -1) {
            if answer[i] < arr[i] {
                return true
            }
            else if answer[i] > arr[i] {
                return false
            }
        }
        return false
    }
    
    var answer = Array(repeating: 0, count: 11)
    var maxScore = 0
   
    func DFS(depth: Int, arrowCount: Int, arr: [Int]) {
        // ๋๊นŒ์ง€ ๋Œ์•˜๊ฑฐ๋‚˜ ํ™”์‚ด์„ ๋‹ค ์ผ์„ ๊ฒฝ์šฐ
        if arrowCount == n || depth == 10 {
            var arr = arr
            
            // ๋‚จ์€ ํ™”์‚ด์ด ์žˆ๋‹ค๋ฉด ๊ทธ ํ™”์‚ด์„ 0์ ์— ๋‹ค ๋„ฃ์–ด์คŒ
            if arrowCount < n {
                arr[10] = n-arrowCount
            }
            
            let score = getScoreDiff(arr) // ์ ์ˆ˜ ์ฐจ์ด๋ฅผ ๊ตฌํ•จ
            
            // ์ ์ˆ˜ ์ฐจ์ด๊ฐ€ ๋” ํฌ๋ฉด ์ •๋‹ต์„ ๋ฐ”๊ฟˆ
            if maxScore < score {
                maxScore = score
                answer = arr
            }
            
            // ํ˜„์žฌ ์ •๋‹ต๊ณผ ์ ์ˆ˜์ฐจ์ด๋Š” ๋˜‘๊ฐ™์ง€๋งŒ ๋‚ฎ์€ ์ ์ˆ˜ํŒ์˜ ํ™”์‚ด์ด ๋” ๋งŽ์œผ๋ฉด ์ •๋‹ต ๋ฐ”๊ฟˆ
            else if maxScore == score && isMoreThanLowScore(answer: answer, arr: arr) {
                answer = arr
            }
        }
        else {
            let apeachWinCount = info[depth] + 1 // ์–ดํ”ผ์น˜๋ฅผ ์ด๊ธธ ์ˆ˜ ์žˆ๋Š” ๊ฐœ์ˆ˜
            
            // ์ง€๊ธˆ ์–ดํ”ผ์น˜๋ฅผ ์ด๊ธธ ์ˆ˜ ์žˆ๋Š” ํ™”์‚ด์ด ๋‚จ์•„์žˆ๋‹ค๋ฉด
            if apeachWinCount+arrowCount <= n {
                var arrWin = arr
                arrWin[depth] = apeachWinCount
                // ์–ดํ”ผ์น˜๋ฅผ ์ด๊ธฐ๋Š” ๊ฒฝ์šฐ DFS
                DFS(depth: depth+1, arrowCount: arrowCount+apeachWinCount, arr: arrWin)
            }
            
            // ์–ดํ”ผ์น˜๋ฅผ ์ง€๋Š” ๊ฒฝ์šฐ DFS (0์œผ๋กœ ์ง„๋‹ค.)
            DFS(depth: depth+1, arrowCount: arrowCount, arr: arr)
        }
    }
    DFS(depth: 0, arrowCount: 0, arr: [0,0,0,0,0,0,0,0,0,0,0])
    
    return maxScore == 0 ? [-1] : answer
}

 

 

ํ›„๊ธฐ

 

ํ‘ธ๋Š”๋ฐ ์ •๋ง ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค... ์ฒ˜์Œ์— 1์‹œ๊ฐ„์ •๋„ ์จ์„œ ํ’€์—ˆ๊ณ 

์‹œ๊ฐ„์ดˆ๊ณผ ๋‚œ๋’ค๋กœ ์–ด๋–ป๊ฒŒ๋“  ์˜ค๊ธฐ๋กœ ํ’€์–ด๋ณด๋ ค๋‹ค๊ฐ€.. ์‚ฐ์œผ๋กœ ๊ฐ

 

๊ฒฐ๊ตญ ์ •๋‹ต๋ณด๊ณ  ์ž˜๋ชป๋œ ์ ‘๊ทผ์ด์—ˆ์Œ์„ ๊นจ๋‹ฌ์•˜๋‹ค.

ํ•˜๋ฃจ ์ด์ƒ ํˆฌ์žํ•˜๋ฉด ๋‹ต์„ ๋ณด์ž

profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...