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

Algorithm/Baekjoon

[λ°±μ€€] (Swift) 14889번 - μŠ€νƒ€νŠΈμ™€ 링크 (DFS-λ°±νŠΈλž˜ν‚Ή)

감자 πŸ₯” 2022. 5. 23. 21:42
λ°˜μ‘ν˜•

문제링크

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

 

14889번: μŠ€νƒ€νŠΈμ™€ 링크

예제 2의 κ²½μš°μ— (1, 3, 6), (2, 4, 5)둜 νŒ€μ„ λ‚˜λˆ„λ©΄ 되고, 예제 3의 κ²½μš°μ—λŠ” (1, 2, 4, 5), (3, 6, 7, 8)둜 νŒ€μ„ λ‚˜λˆ„λ©΄ λœλ‹€.

www.acmicpc.net

 

 

λ¬Έμ œν’€μ΄

ν•΄λ‹Ή 문제λ₯Ό ν’€κΈ° μœ„ν•΄μ„œλŠ” μ™„μ „νƒμƒ‰μœΌλ‘œ ν’€κ±°λ‚˜, λ°±νŠΈλž˜ν‚Ήμ΄λΌλŠ” κ°œλ…μ„ μ΄μš©ν•˜λŠ” 방법이 μžˆλ‹€κ³  ν•œλ‹€. 완전탐색은 λ§κ·ΈλŒ€λ‘œ κ·Έλž˜ν”„ 전체λ₯Ό νƒμƒ‰ν•΄μ„œ νŒ€μ›μ΄ λλŠ”μ§€, μ•ˆλλŠ”μ§€ νŒλ‹¨ν•˜λŠ” 방법이고, λ°±νŠΈλž˜ν‚Ήμ€ "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))

 

 

πŸ’» μ΅œμ’… μ½”λ“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Boj/14889_%EC%8A%A4%ED%83%80%ED%8A%B8%EC%99%80%20%EB%A7%81%ED%81%AC/main.swift

 

GitHub - deslog/Algorithm

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

github.com

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)
λ°˜μ‘ν˜•