1. ๋ฌธ์
2. ์ ์ถ๋ ฅ
3. ์ ์ถ๋ ฅ ์์
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()
์ ์ฒด์ฝ๋๋ ์ฌ๊ธฐ์์ ํ์ธํ ์ ์์ต๋๋ค.
'Swift Data Structure And Algorithm > Graph Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ต๋จ๊ฑฐ๋ฆฌ (0) | 2022.04.16 |
---|---|
์ต๋จ ๊ฒฝ๋ก ์๊ณ ๋ฆฌ์ฆ - ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ(Dijkstra's Algorithm) (0) | 2022.04.16 |
์ ์ผ๋ณ (0) | 2022.04.15 |
์ด์ํ ๊ณ์ฐ๊ธฐ (0) | 2022.04.15 |
๋จ์ง ๋ฒํธ ๋ถ์ด๊ธฐ_BFS (0) | 2022.04.15 |