λ¬Έμ λ§ν¬
https://www.acmicpc.net/problem/14889
λ¬Έμ νμ΄
ν΄λΉ λ¬Έμ λ₯Ό νκΈ° μν΄μλ μμ νμμΌλ‘ νκ±°λ, λ°±νΈλνΉμ΄λΌλ κ°λ μ μ΄μ©νλ λ°©λ²μ΄ μλ€κ³ νλ€. μμ νμμ λ§κ·Έλλ‘ κ·Έλν μ 체λ₯Ό νμν΄μ νμμ΄ λλμ§, μλλμ§ νλ¨νλ λ°©λ²μ΄κ³ , λ°±νΈλνΉμ "DFS λ°©λ²μ΄μ§λ§, μ΄λμ λμ depthμ λλ¬νκ² λλ©΄ νμμ€μ§"νλ μκ³ λ¦¬μ¦μ΄λ€. λ°±νΈλνΉμ λν κ°λ μ μ λͺ¨λ₯΄λ, μ΄ λ¬Έμ λ₯Ό ν¬μ€ν ν ν λ°±νΈλνΉκ³Ό κ΄λ ¨ν μκ³ λ¦¬μ¦ ν¬μ€ν μ ν μμ μ΄λ€.
μ°μ λλ, λ°±νΈλνΉμ΄ λμ§ λͺ¨λ₯΄κ² κ³ !! dfsλ₯Ό ꡬννμ§λ§, μΌμ μ«μκ° λλ€λ©΄, νμ ꡬμ±μ λ©μΆκ³ μ°¨μ΄λ₯Ό ꡬνλ€. (μ΄κ²λ°λ‘ λ°±νΈλνΉ!!)
1. νꡬμ±μ μν λ³μ μ€μ
- n : μ λ ₯λ°λ λ³μ
- s : μ λ ₯λ°λ λ₯λ ₯μΉ
- visited : λ°©λ¬Έμ²λ¦¬ (ν ꡬμ±μ΄ μλ£λ κ²μ true, μλκ²μ falseλ‘ λ μμ )
- team1 : team1μ΄ λ μ¬λλ€μ λ₯λ ₯μΉ ν©
- team2 : team2κ° λ μ¬λλ€μ λ₯λ ₯μΉ ν©
- minResult : μμ κ°μ κ³μν΄μ κ°±μ ν μμ , (μ΄λ°μ ν°μ μμλ‘ μ€μ )
let n = Int(String(readLine()!))!
var s = Array(repeating: Array(repeating: 0, count: n), count: n)
var visited = Array(repeating: false, count: n)
var team1 = 0
var team2 = 0
var minResult = 99999
2. dfs ν¨μ
- iλΆν° νμνλ©΄μ visited μ true λ‘ λ°κΎΈλ©΄μ νμ
for i in start..<n {
if !visited[i] {
visited[i] = true
dfs(depth: depth + 1, start: i)
visited[i] = false
}
}
- depth κ° n/2 κ° λλ μκ°, dfsμ νμμ μ€μ§ν κ²μ΄λ€. κ·Έλμ if λ¬Έμ dfs ν¨μ λ΄λΆ μ΅μλ¨μ μμΉμν΄
- κ³μν΄μ κ°±μ λμ΄μ κ°μ± λ³νλ team1κ³Ό team2 λ₯Ό μ΄κΈ°ν μμΌμ£Όκ³ ,
- for λ¬Έμ λλ €μ λ°©λ¬Ένλ (true)μΈ μ λ€μ team1λ‘, λ°©λ¬Ένμ§ μμλ (false)μΈ μ λ€μ team2λ‘ λ³΄λ΄λ©΄μ λ₯λ ₯μΉκ°μ λν΄μ€
- λ§μ§λ§μΌλ‘ team1μμ team2λ₯Ό λΊ μ λκ°μ minResultμ λΉκ΅ν΄μ λ£μ΄μ€.
// depth κ° λ°μ΄λλ€λ λ»μ μ΄λ―Έ ν ν ꡬμ±μ΄ μλ£ λλ€λ λ»
// κ·Έλμ depth/2 κ° λλ©΄, νꡬμ±λͺ»νμ λ€μ(false) team2μ λ£μ΄μ€!!
if depth == n/2 {
team1 = 0
team2 = 0
for i in 0..<n{
for j in 0..<n{
if !visited[i] && !visited[j]{
team2 += s[i][j]
}
if visited[i] && visited[j] {
team1 += s[i][j]
}
}
}
// κ·Έλ¦¬κ³ νꡬμ±μ΄ μλ£λ μ λ€μ μ°¨λ₯Ό ꡬν΄μ minκ°μ κ³μ κ°±μ μμΌμ£Όμ
minResult = min(minResult, abs(team1 - team2))
π» μ΅μ’ μ½λ
let n = Int(String(readLine()!))!
var s = Array(repeating: Array(repeating: 0, count: n), count: n)
var visited = Array(repeating: false, count: n)
var team1 = 0
var team2 = 0
var minResult = 99999
for i in 0..<n {
s[i] = readLine()!.split(separator: " ").map { Int(String($0))! }
}
func dfs(depth: Int, start: Int) {
// depth κ° λ°μ΄λλ€λ λ»μ μ΄λ―Έ ν ν ꡬμ±μ΄ μλ£ λλ€λ λ»
// κ·Έλμ depth = n/2 κ° λλ©΄, νꡬμ±λͺ»νμ λ€μ(false) team2μ λ£μ΄μ€!!
if depth == n/2 {
team1 = 0
team2 = 0
for i in 0..<n{
for j in 0..<n{
if !visited[i] && !visited[j]{
team2 += s[i][j]
}
if visited[i] && visited[j] {
team1 += s[i][j]
}
}
}
// κ·Έλ¦¬κ³ νꡬμ±μ΄ μλ£λ μ λ€μ μ°¨λ₯Ό ꡬν΄μ minκ°μ κ³μ κ°±μ μμΌμ£Όμ
minResult = min(minResult, abs(team1 - team2))
return
}
for i in start..<n {
if !visited[i] {
visited[i] = true
dfs(depth: depth + 1, start: i)
visited[i] = false
}
}
}
dfs(depth: 0, start: 0)
print(minResult)
'Algorithm > Baekjoon' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λ°±μ€](Swift) 2667λ² - λ¨μ§λ²νΈλΆμ΄κΈ° (DFSμ κ°μ₯ κΈ°λ³Έμ μΈ λ¬Έμ ) (1) | 2022.05.27 |
---|---|
[λ°±μ€] (Swift) 1707λ² - μ΄λΆκ·Έλν (DFSλ‘ νκΈ°!) (0) | 2022.05.25 |
[λ°±μ€] (Swift) 11724λ² - μ°κ²° μμμ κ°μ (DFS μ°μ΅) (0) | 2022.05.20 |
[λ°±μ€] (Swift) 14501λ² - ν΄μ¬ (DPλ‘ νκΈ°) (0) | 2022.05.17 |
[λ°±μ€] (Swift) 1260λ² - DFSμ BFS (0) | 2022.05.14 |