π λ¬Έμ λ§ν¬
https://www.acmicpc.net/problem/1679
π νμ΄λ°©μ
μΈν°λ·μ 보λκΉ 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 μ λ£μ΄μ£Όμλ€.
//
// 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
}
}