๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/๋ฐฑ์ค€

[๋ฐฑ์ค€] Swift ์ผ€๋นˆ ๋ฒ ์ด์ปจ์˜ 6๋‹จ๊ณ„ ๋ฒ•์น™ BFS

by Jouureee 2022. 4. 17.

๋ฌธ์ œ :

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

 

1389๋ฒˆ: ์ผ€๋นˆ ๋ฒ ์ด์ปจ์˜ 6๋‹จ๊ณ„ ๋ฒ•์น™

์ฒซ์งธ ์ค„์— ์œ ์ €์˜ ์ˆ˜ N (2 ≤ N ≤ 100)๊ณผ ์นœ๊ตฌ ๊ด€๊ณ„์˜ ์ˆ˜ M (1 ≤ M ≤ 5,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์—๋Š” ์นœ๊ตฌ ๊ด€๊ณ„๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์นœ๊ตฌ ๊ด€๊ณ„๋Š” A์™€ B๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, A์™€ B๊ฐ€ ์นœ๊ตฌ๋ผ๋Š” ๋œป

www.acmicpc.net

 

๋ถ„์„ :

์ฒ˜์Œ์—๋Š” ์™œ ์ด๊ฒŒ BFS์ง€ ? ๊นŠ์ด์— ๋”ฐ๋ผ ๋‚ด๋ ค๊ฐ€๋‹ˆ๊นŒ๋Š” DFS ์•„๋‹Œ๊ฐ€ ์‹ถ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‹ค์‹œ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ์šฐ๋ฆฌ๋Š” ๊นŠ์ด๊ฐ€ ๋‚ฎ์€ ์นœ๊ตฌ ๊ด€๊ณ„๊ฐ€ ์žˆ๋‹ค๋ฉด ์ด ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๊นŠ์ด๊ฐ€ ๋‚ฎ์€ ๊ณณ์— ์›ํ•˜๋Š” ์›์†Œ๊ฐ€ ์žˆ๋‹ค๋ฉด visit๋ฅผ true๋กœ ๋งŒ๋“ค์–ด ๋” ๊นŠ์€ ๊ณณ์— ์›ํ•˜๋Š” ์›์†Œ๊ฐ€ ์žˆ์–ด๋„ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๊ณ  ๋„˜์–ด๊ฐˆ์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค.

 

๋˜ํ•œ ์ž๊ธฐ ์ž์‹ ์„ ์ œ์™ธํ•œ ๋ชจ๋“  ์นœ๊ตฌ์˜ ๊ด€๊ณ„(๊นŠ์ด)๋ฅผ ์•Œ๊ณ  ์‹ถ์€ ๊ฒƒ์œผ๋กœ for๋ฌธ์„ ๋Œ๋•Œ ์ž๊ธฐ ์ž์‹ ์˜ ๊นŠ์ด๊ฐ€ 1์ธ ์นœ๊ตฌ๊ฐ€ ์•„๋‹Œ 1 ~ N๊นŒ์ง€ ๋ชจ๋‘ ๊ฒ€์‚ฌํ•ด์•ผ ํ•œ๋‹ค. ์ด ์ ์„ ์ฐธ๊ณ ํ•˜์—ฌ queue ์ธ์ž์—  queue(start: Int, find: Int, cnt: Int) ๋ฅผ queue(start: Int, cnt: Int) ์‹œ์ž‘ ์›์†Œ๋งŒ ์—…๋ฐ์ดํŠธ ๋˜๊ฒŒ๋” ์ˆ˜์ •ํ•˜์—ฌ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

ํ‹€๋ฆฐ ์ ‘๊ทผ ์ฝ”๋“œ :

import Foundation
var line = readLine()!.split(separator: " ").map { Int(String($0))! }
var n = line[0]
var m = line[1]
var tree: [[Int]] = Array(repeating: [], count: n+1)
var visit: [Int] = Array(repeating: 0, count: n+1)

for _ in 0..<m {
    var l = readLine()!.split(separator: " ").map { Int(String($0))! }
    tree[l[0]].append(l[1])
    tree[l[1]].append(l[0])
}

func bfs_level(start: Int, find: Int, cnt: Int) {
    var queue = [(Int, Int, Int)]()
    visit[start] = 1
    queue.append((start, find, cnt))

    while !queue.isEmpty {
        let now = queue.first!.0
        let value = queue.first!.1
        let cn = queue.first!.2
        queue.removeFirst()
        
        if value > n { break }
        
        if tree[now].contains(value) {
            visit[value] = cn
            queue.append((start, value+1, 1))
            continue
        }

        for i in 0..<tree[now].count {
            var next = tree[now][i]

            if visit[next] == 0 {
                queue.append((next, value, cnt+1))
            }
        }
    }
}

 

Swift ์ฝ”๋“œ :

//
//  main.swift
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2022/04/17.
//

import Foundation
var line = readLine()!.split(separator: " ").map { Int(String($0))! }
var n = line[0]
var m = line[1]
var tree: [[Int]] = Array(repeating: Array(repeating: 0, count: n+1), count: n+1)

for _ in 0..<m {
    let l = readLine()!.split(separator: " ").map { Int(String($0))! }
    tree[l[0]][l[1]] = 1
    tree[l[1]][l[0]] = 1
}


func bfs_level(start: Int, cnt: Int) -> Int {
    var visit: [Bool] = Array(repeating: false, count: n+1)
    var queue = [(Int, Int)]()
    var answer = 0
    visit[start] = true
    queue.append((start, cnt))
    
    while !queue.isEmpty {
        let now = queue.first!.0
        let cnt = queue.first!.1
        queue.removeFirst()
        
        for i in 1..<n+1 {
            if !visit[i] && tree[now][i] == 1 {
                answer += cnt
                queue.append((i, cnt + 1))
                visit[i] = true
            }
        }
    }
    
    return answer
}

var answer = 0
var min = 10000
for i in 1..<n {
    let temp = bfs_level(start: i, cnt: 1)
    if(temp < min) {
        min = temp
        answer = i
    }
}
print(answer)

๋Œ“๊ธ€