Potato
μ•ˆλ…•ν•˜μ„Έμš”, κ°μž‘λ‹ˆλ‹€?πŸ₯” ^___^ 😺 github λ°”λ‘œκ°€κΈ° πŸ‘‰πŸ»

Algorithm/Baekjoon

[λ°±μ€€] (Swift) 1679번 - μˆ«μžλ†€μ΄ (dp)

감자 πŸ₯” 2022. 7. 28. 16:36
λ°˜μ‘ν˜•

🟠 문제링크

https://www.acmicpc.net/problem/1679

 

1679번: μˆ«μžλ†€μ΄

ν™€μˆœμ΄(holsoon)와 짝순이(jjaksoon) λ‘˜μ΄μ„œ 숫자 κ²Œμž„μ„ ν•œλ‹€. 예λ₯Ό λ“€μ–΄, μ •μˆ˜ 1κ³Ό 3이 주어지고, 이 λ‘˜μ„ 톡틀어 5λ²ˆκΉŒμ§€ λ§ˆμŒλŒ€λ‘œ μ‚¬μš©ν•˜μ—¬ κ·Έ 합을 κ΅¬ν•˜μ—¬ 1,2,3,…을 λ§Œλ“œλŠ” 놀이닀. 이 경우 λ¨Όμ €

www.acmicpc.net

 

🟠 풀이방식

인터넷을 λ³΄λ‹ˆκΉŒ dp, dfs λ“± λ‹€μ–‘ν•œ λ°©μ‹μœΌλ‘œ ν’€ 수 μžˆλŠ” 문제 κ°™μ•˜λ‹€. λ‚˜λŠ” κ·Έλƒ₯ κ°€μž₯ λ¨Όμ € λ– μ˜¬λžλ˜ dp 둜 문제λ₯Ό ν’€μ—ˆλ‹€.

dp μ—μ„œ κ°€μž₯ μ€‘μš”ν•œ 것은, ν•΄λ‹Ή μΈλ±μŠ€μ— μ–΄λ–€ value값을 λ„£λŠλƒλ₯Ό κ²°μ •ν•˜λŠ”κ²Œ κ°€μž₯ μ€‘μš”ν•˜λ‹€. λ‚˜λ˜ν•œ μ—¬κΈ°μ„œ μ–΄λ–»κ²Œ 넣을지 많이 κ³ λ―Όν–ˆλ˜ 것 κ°™λ‹€.

μš°μ„  dp[i] 라고 μ³€μ„λ•Œ, i번 λ”ν•΄μ„œ λ‚˜μ˜¬ 수 μžˆλŠ” μˆ˜λ“€μ„ dp μ•ˆμ— λ„£μ–΄μ£Όμ—ˆλ‹€. κ·Έλ‹ˆκΉŒ, μˆ˜λ“€μ΄λ‹ˆκΉŒ array ν˜•νƒœλ‘œ value 값을 λ„£μ–΄μ£Όμ—ˆλ‹€. 예제둜 예λ₯Ό λ“€λ©΄, 1κ³Ό 3이 1λ²ˆλ”ν•΄μ„œ λ‚˜μ˜¬ 수 μžˆλŠ” μˆ˜μ΄λ‹ˆκΉŒ, dp[1] = [1, 3] μ΄λ ‡κ²Œ λ„£μ–΄μ€€ 것이닀.

μžμ„Έν•œ 풀이 방법은 μ•„λž˜μ™€ κ°™λ‹€.

 

🟠 λ©”λͺ¨λ¦¬ 초과 μ½”λ“œ

μ™œ λ©”λͺ¨λ¦¬ μ΄ˆκ³Όκ°€ λ‚¬μ„κΉŒ μƒκ°ν•΄λ³΄λ‹ˆκΉŒ, μœ„ λ‚΄κ°€ κ·Έλ¦° 그림처럼, dp에 λͺ¨λ“ !!! 값듀을 μ€‘λ³΅ν•΄μ„œ λ„£μ–΄μ£Όκ³  μžˆμ—ˆλ‹€. μ—¬κΈ°μ„œ 쀑볡을 κ±ΈλŸ¬μ£Όμ§€ μ•Šμ•„μ„œ λ©”λͺ¨λ¦¬κ°€ λ„ˆλ¬΄ λ°©λŒ€ν•΄μ§€λ‹ˆκΉŒ λ©”λͺ¨λ¦¬μ΄ˆκ³Όκ°€ λ‚œ 것 κ°™μ•„μ„œμˆ˜μ •ν–ˆλ‹€.

//
//  main.swift
//  Algorithm
//
//  Created by LeeJiSoo on 2022/07/28.
//

import Foundation

let n = Int(readLine()!)!
let nums = readLine()!.split(separator: " ").map { Int(String($0))! }
let k = Int(readLine()!)!
var dp = Array(repeating: [Int](), count: k+1)
var visited = Array(repeating: false, count: (nums.max()! * k)+1)

for num in nums {
    visited[num] = true
    dp[1].append(num)
}

for i in 2..<k+1 {
    let previous = dp[i-1]
    for p in previous {
        for num in nums {
            visited[num+p] = true
            // βœ… μ—¬κΈ°μ„œ λ„ˆλ¬΄ λͺ¨λ“  μˆ˜λ“€μ„ dp에 λ•Œλ €λ°•κ³ μžˆμ—ˆλ‹€.
            dp[i].append(num+p)
        }
    }
}

for j in 1..<(nums.max()!*k)+1 {
    if visited[j] == false {
        print(j % 2 == 0 ? "holsoon win at \(j)" : "jjaksoon win at \(j)")
        break
    }
}

 

🟠 μ „μ²΄μ½”λ“œ - λ©”λͺ¨λ¦¬μ΄ˆκ³Ό ν•΄κ²°

dp.contains() λ₯Ό μ΄μš©ν•΄μ„œ dp 에 λ¨Όμ € 값이 μžˆλŠ”μ§€ νŒλ‹¨ν•΄μ£Όκ³ , 없을 κ²½μš°μ—λ§Œ dp 에 λ„£μ–΄μ£Όμ—ˆλ‹€. 

https://github.com/deslog/Algorithm/blob/main/Algorithm/Boj/1679_%EC%88%AB%EC%9E%90%EB%86%80%EC%9D%B4/main.swift

 

GitHub - deslog/Algorithm

Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

//
//  main.swift
//  Algorithm
//
//  Created by LeeJiSoo on 2022/07/28.
//

import Foundation

let n = Int(readLine()!)!
let nums = readLine()!.split(separator: " ").map { Int(String($0))! }
let k = Int(readLine()!)!
var dp = Array(repeating: [Int](), count: k+1)
var visited = Array(repeating: false, count: (nums.max()! * k)+1)

for num in nums {
    visited[num] = true
    dp[1].append(num)
}

for i in 2..<k+1 {
    let previous = dp[i-1]
    for p in previous {
        for num in nums {
            visited[num+p] = true
            if !dp[i].contains(num+p) {
                dp[i].append(num+p)
            }
        }
    }
}

for j in 1..<(nums.max()!*k)+1 {
    if visited[j] == false {
        print(j % 2 == 0 ? "holsoon win at \(j)" : "jjaksoon win at \(j)")
        break
    }
}

 

λ°˜μ‘ν˜•