Swift Data Structure And Algorithm/Graph Algorithm

๋ชฉ์ˆ˜์˜ ๋ฏธ๋กœ ํƒˆ์ถœ

youngjaeLee1026 2022. 4. 15. 13:20

1. ๋ฌธ์ œ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-04-15 13 08 30

2. ์ž…์ถœ๋ ฅ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-04-15 13 08 38

3. ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-04-15 13 08 48แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-04-15 13 08 55

4. ๋ฌธ์ œ ์„ค๊ณ„

  • ์œ„ ๋ฌธ์ œ๋Š” ์ด์ „ ๋ฏธ๋กœ ์ฐพ๊ธฐ ๋ฌธ์ œ๋ฅผ ์‘์šฉํ•œ ๋ฌธ์ œ๋กœ, ์‹œ์ž‘์ ์—์„œ ๊ฐ ๋ฒฝ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ, ๋„์ฐฉ์ง€์ ์—์„œ ๊ฐ ๋ฒฝ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ๊ฐ ๋ฐฐ์—ด์— ์ €์žฅํ•˜๊ณ , ๋‘ ๋ฐฐ์—ด์— ๋Œ€ํ•ด ๋ฒฝ์— ํ•ด๋‹น๋˜๋Š” ๊ฐ’์˜ ํ•ฉ์˜ ์ตœ์†Œ๊ฐ’์„ ์ฐพ์Œ์œผ๋กœ์จ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Œ
  • ์ฃผ์˜ํ•  ์ ์€, ๊ฐ๊ฐ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด BFS๋ฅผ ์ง„ํ–‰ํ•  ๋•Œ, ๋ฏธ๋กœ์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๊ณ  ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ๊ฒ€์‚ฌํ•˜๋ฉด์„œ ํƒ์ƒ‰ํ•˜๋˜, ๋ฒฝ์— ํ•ด๋‹น๋˜๋Š” ๋ถ€๋ถ„์€ ๋ฐฉ๋ฌธํ•˜๋ฉด ์•ˆ๋จ
  • ์œ„ ๊ณผ์ •์„ ํ†ตํ•ด ๊ฐ๊ฐ์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๊ณ , ๋‘ ๋ฐฐ์—ด์˜ ํ•ฉ์˜ ์ตœ์†Œ๊ฐ’์„ ์ฐพ๊ธฐ ์œ„ํ•ด ์™„์ „ํƒ์ƒ‰์„ ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด ๋•Œ ํŠน์ • ๋ฒฝ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๊ฐ’์ด 0์ผ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ด์— ๋Œ€ํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•จ
  • ๋”ฐ๋ผ์„œ O(N * M)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Œ

5. ์ „์ฒด ์ฝ”๋“œ

//MARK: - ๋ชฉ์ˆ˜์˜ ๋ฏธ๋กœ ํƒˆ์ถœ

//MARK: - Framework
import Foundation

//MARK: - Type
struct Node {
    //MARK: - Property
    var x, y: Int
    
    //MARK: - Initializer
    init(_ x: Int, _ y: Int) {
        self.x = x
        self.y = y
    }
}

//MARK: - Variable
var N: Int = 0
var M: Int = 0
var graph: [[Int]] = []
var visited: [[Bool]] = []
var costOne: [[Int]] = []
var costTwo: [[Int]] = []
var dx: [Int] = [0, 0, -1, 1]
var dy: [Int] = [-1, 1, 0, 0]

//MARK: - Function
func bfs(_ x: Int, _ y: Int, _ cost: inout [[Int]]) -> Void {
    var queue: [Node] = []
    queue.append(Node(x, y))
    visited[x][y] = true
    
    while !queue.isEmpty {
        let currentNode: Node = queue.removeFirst()
        
        for i in 0..<4 {
            let nx: Int = currentNode.x + dx[i]
            let ny: Int = currentNode.y + dy[i]
            
            if 1 <= nx && nx <= N && 1 <= ny && ny <= M && !visited[nx][ny] {
                visited[nx][ny] = true
                cost[nx][ny] = cost[currentNode.x][currentNode.y] + 1
                
                if graph[nx][ny] == 0 {
                    queue.append(Node(nx, ny))
                }
            }
        }
    }
}

func solution() -> Void {
    //MARK: - Input
    guard let input: [String] = readLine()?.components(separatedBy: " ") else { return }
    var result: Int = 987987987
    
    N = input.map { Int($0) }[0] ?? 0
    M = input.map { Int($0) }[1] ?? 0
    graph = Array(repeating: Array(repeating: 1, count: M + 10), count: N + 10)
    visited = Array(repeating: Array(repeating: false, count: M + 10), count: N + 10)
    costOne = Array(repeating: Array(repeating: 0, count: M + 10), count: N + 10)
    costTwo = Array(repeating: Array(repeating: 0, count: M + 10), count: N + 10)
    
    for i in 1...N {
        guard let inputData: [String] = readLine()?.components(separatedBy: " ") else { return }
        let data: [Int] = inputData.map { Int($0) ?? 0 }
        
        for j in 1...M {
            graph[i][j] = data[j - 1]
        }
    }
    
    //MARK: - Process
    bfs(N, 1, &costOne)
    visited = Array(repeating: Array(repeating: false, count: M + 10), count: N + 10)
    bfs(1, M, &costTwo)
    
    for i in 1...N {
        for j in 1...M {
            if graph[i][j] == 1 && costOne[i][j] > 0 && costTwo[i][j] > 0 {
                result = result > costOne[i][j] + costTwo[i][j] ? costOne[i][j] + costTwo[i][j] : result
            }
        }
    }
    
    //MARK: - Output
    print(result)
}
solution()

 

์ „์ฒด์ฝ”๋“œ๋Š” ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.