๋ฐ์ํ
BFS(Breadth First Search)
๋ค์ฐจ์ ๋ฐฐ์ด์์ ๊ฐ ์นธ์ ๋ฐฉ๋ฌธํ ๋ ๋๋น๋ฅผ ์ฐ์ ์ผ๋ก ๋ฐฉ๋ฌธํ๋ ์๊ณ ๋ฆฌ์ฆ
๊ตฌํ๋ฐฉ๋ฒ
1. ์์ํ๋ ์นธ์ ํ์ ๋ฃ๊ณ ๋ฐฉ๋ฌธํ๋ค๋ ํ์๋ฅผ ๋จ๊น
2. ํ์์ ์์๋ฅผ ๊บผ๋ด์ด ๊ทธ ์นธ์ ์ํ์ข์ฐ๋ก ์ธ์ ํ ์นธ์ ๋ํด 3๋ฒ์ ์งํ
3. ํด๋น ์นธ์ ์ด์ ์ ๋ฐฉ๋ฌธํ๋ค๋ฉด ์๋ฌด ๊ฒ๋ ํ์ง ์๊ณ , ์ฒ์์ผ๋ก ๋ฐฉ๋ฌธํ๋ค๋ฉด ๋ฐฉ๋ฌธํ๋ค๋ ํ์๋ฅผ ๋จ๊ธฐ๊ณ ํด๋น ์นธ์ ํ์ ์ฝ์
4. ํ๊ฐ ๋น ๋๊น์ง 2๋ฒ์ ๋ฐ๋ณต
๋ชจ๋ ์นธ์ด ํ์ 1๋ฒ์ฉ ๋ค์ด๊ฐ๋ฏ๋ก ์๊ฐ๋ณต์ก๋๋ ์นธ์ด N๊ฐ์ผ ๋ O(N).
์์์ฝ๋
import Foundation
let board: [[Int]] = [
[1,1,1,0,1,0,0,0,0,0],
[1,0,0,0,1,0,0,0,0,0],
[1,1,1,0,1,0,0,0,0,0],
[1,1,0,0,1,0,0,0,0,0],
[0,1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]
]
var vis = Array(repeating: Array(repeating: false, count: 502), count: 502)
let n = 7, m = 10
let dx = [1, 0, -1, 0]
let dy = [0, 1, 0, -1]
var queue = [(Int, Int)]()
vis[0][0] = true
queue.append((0, 0))
while !queue.isEmpty {
let cur = queue.removeFirst()
print("(\(cur.0), \(cur.1)) ", terminator: "")
for dir in 0..<4 {
let nx = cur.0 + dx[dir]
let ny = cur.1 + dy[dir]
if nx < 0 || nx >= n || ny < 0 || ny >= m {
continue
}
if vis[nx][ny] || board[nx][ny] != 1 {
continue
}
vis[nx][ny] = true
queue.append((nx, ny))
}
}
print("")
์์ฉ 1 - ๊ฑฐ๋ฆฌ ์ธก์
๋ค์ฐจ์ ๋ฐฐ์ด์์ ๊ฑฐ๋ฆฌ ์ธก์ ์ ํตํด ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ ์ ํ
์ด๋ฐ์์ผ๋ก ๊ฑฐ๋ฆฌ๋ฅผ DFS๋ก ์ ์ด์ ์ต๋จ๊ฒฝ๋ก๋ฅผ ๊ตฌํ๋ค.
import Foundation
let dx: [Int] = [1, 0, -1, 0]
let dy: [Int] = [0, 1, 0, -1]
let input = readLine()!.split(separator: " ").map { Int($0)! }
let n = input[0]
let m = input[1]
var board: [[String]] = []
for _ in 0..<n {
let row = readLine()!.map { String($0) }
board.append(row)
}
var dist: [[Int]] = Array(repeating: Array(repeating: -1, count: m), count: n)
var queue: [(Int, Int)] = []
queue.append((0, 0))
dist[0][0] = 0
while !queue.isEmpty {
let cur = queue.removeFirst()
for dir in 0..<4 {
let nx = cur.0 + dx[dir]
let ny = cur.1 + dy[dir]
if nx < 0 || nx >= n || ny < 0 || ny >= m {
continue
}
if dist[nx][ny] >= 0 || board[nx][ny] != "1" {
continue
}
dist[nx][ny] = dist[cur.0][cur.1] + 1
queue.append((nx, ny))
}
}
print(dist[n-1][m-1] + 1)
์์ฉ2 - ์์์ ์ด ์ฌ๋ฌ ๊ฐ์ผ ๋
์์์ ์ด ์ฌ๋ฌ๊ฐ์ธ ์ํฉ์ผ ๋๋ ๋ชจ๋ ์์์ ์ ํ์ ๋ฃ๊ณ BFS๋ฅผ ๋๋ฆฐ๋ค.
์์ BOJ 7576 ํ ๋งํ
1. ๋ชจ๋ ์์์ ์ ์ด์ค for๋ฌธ์ ๋ฃ๊ณ ํ์ ๋ฃ์ด๋๋ค.
2. bfs๋ฅผ ๋๋ฆฌ๋ฉด์ ๋ง์ง๋ง index๋ฅผ ์ ์ฅํด๋๋ค.
3. 0์ด ์๋์ง ์ฌ๋ถ๋ฅผ ์ฒดํฌํด์ ๋ง์ฝ ๋ฐ๋์ง ์์ ํ ๋งํ ๊ฐ ์๋ค๋ฉด -1์ ์ถ๋ ฅํ๊ฒ ํ๋ค.
import Foundation
let dx: [Int] = [1, 0, -1, 0]
let dy: [Int] = [0, 1, 0, -1]
let input = readLine()!.split(separator: " ").map { Int($0)! }
let m = input[0]
let n = input[1]
var board: [[Int]] = []
var dist: [[Int]] = []
var queue: [(Int, Int)] = []
var idx = 0
var lastIdx = [0, 0]
var isCooked = true
for _ in 0..<n {
let row = readLine()!.split(separator: " ").map { Int($0)! }
board.append(row)
dist.append(Array(repeating: -1, count: m))
}
for i in 0..<n {
for j in 0..<m {
if board[i][j] == 1 {
queue.append((i, j))
}
}
}
while idx < queue.count {
let cur = queue[idx]
idx += 1
for dir in 0..<4 {
let nx = cur.0 + dx[dir]
let ny = cur.1 + dy[dir]
if nx<0 || nx>=n || ny<0 || ny>=m { continue }
if board[nx][ny] == 0 {
board[nx][ny] = 1
queue.append((nx, ny))
dist[nx][ny] = dist[cur.0][cur.1] + 1
lastIdx = [nx, ny]
}
}
}
for i in 0..<n {
for j in 0..<m {
if board[i][j] == 0 {
isCooked = false
}
}
}
if isCooked {
print(dist[lastIdx[0]][lastIdx[1]]+1)
} else {
print(-1)
}
์์ฉ3 - ์์์ ์ด ๋ ์ข ๋ฅ์ผ ๋
๋๊ฐ์ง ์ข ๋ฅ์ ๋ํด ๋ชจ๋ BFS๋ฅผ ๋๋ ค์ ํด๊ฒฐํ๋ค. q์ dist๋ฅผ 2๊ฐ์ฉ ๋ง๋ฌ
BOJ 4179 ๋ถ
1. ์ฐ์ ์งํ์ด๋ ์ ๊ฒฝ์ฐ์ง๋ง๊ณ ๋ถ์ ๋ํ BFS๋ฅผ ๋๋ ค์ ๋ฏธ๋ฆฌ ๊ฐ ์นธ์ ๋ถ์ด ์ ํ๋๋ ์๊ฐ์ ๊ตฌํ๋ค.
2. ์งํ์ด์ ๋ํ BFS๋ฅผ ๋๋ฆฌ๋ฉฐ ์งํ์ด๋ฅผ ์ด๋์ํจ๋ค.
import Foundation
let dx = [1, 0, -1, 0]
let dy = [0, 1, 0, -1]
let nm = readLine()!.split(separator: " ").map { Int($0)! }
let n = nm[0]
let m = nm[1]
var board = [[String]]()
var dist1 = Array(repeating: Array(repeating: -1, count: m), count: n) // ๋ถ์ ์ ํ ์๊ฐ
var dist2 = Array(repeating: Array(repeating: -1, count: m), count: n) // ์งํ์ด์ ์ด๋ ์๊ฐ
for _ in 0..<n {
let line = readLine()!.map { String($0) }
board.append(line)
}
var q1 = [(Int, Int)]()
var q2 = [(Int, Int)]()
for i in 0..<n {
for j in 0..<m {
if board[i][j] == "F" {
q1.append((i, j))
dist1[i][j] = 0
}
if board[i][j] == "J" {
q2.append((i, j))
dist2[i][j] = 0
}
}
}
// ๋ถ์ ๋ํ BFS
var q1Idx = 0
while q1Idx < q1.count {
let cur = q1[q1Idx]
q1Idx += 1
for dir in 0..<4 {
let nx = cur.0 + dx[dir]
let ny = cur.1 + dy[dir]
if nx < 0 || nx >= n || ny < 0 || ny >= m {
continue
}
if dist1[nx][ny] >= 0 || board[nx][ny] == "#" {
continue
}
dist1[nx][ny] = dist1[cur.0][cur.1] + 1
q1.append((nx, ny))
}
}
// ์งํ์ด์ ๋ํ BFS
var q2Idx = 0
while q2Idx < q2.count {
let cur = q2[q2Idx]
q2Idx += 1
for dir in 0..<4 {
let nx = cur.0 + dx[dir]
let ny = cur.1 + dy[dir]
if nx < 0 || nx >= n || ny < 0 || ny >= m {
// ๋ฒ์๋ฅผ ๋ฒ์ด๋ฌ๋ค๋ ๊ฒ์ ํ์ถ์ ์ฑ๊ณตํ๋ค๋ ์๋ฏธ. ํ์ ๊ฑฐ๋ฆฌ ์์ผ๋ก ๋ค์ด๊ฐ๋ฏ๋ก ์ต์ด์ ํ์ถํ ์๊ฐ์ ์ถ๋ ฅํ๋ฉด ๋จ.
print(dist2[cur.0][cur.1] + 1)
exit(0)
}
if dist2[nx][ny] >= 0 || board[nx][ny] == "#" {
continue
}
// ๋ถ์ด ์ง๋๊ฐ ์๊ฐ์ด ์งํ์ด๊ฐ ์ง๋๊ฐ ์๊ฐ๋ณด๋ค ์์์ผํจ
if dist1[nx][ny] != -1 && dist1[nx][ny] <= dist2[cur.0][cur.1] + 1 {
continue
}
dist2[nx][ny] = dist2[cur.0][cur.1] + 1
q2.append((nx, ny))
}
}
print("IMPOSSIBLE") // ์ฌ๊ธฐ์ ๋๋ฌํ๋ค๋ ๊ฒ์ ํ์ถ์ ์คํจํ์์ ์๋ฏธ.
์์ฉ4 - 1์ฐจ์์์์ BFS
2์ฐจ์ ๋ฐฐ์ด์์ ์ํ์ข์ฐ๋ก ๋ฐฉํฅ์ ๋์๋ฏ์ด ๋ฌธ์ ์กฐ๊ฑด์ ๋ฐ๋ผ BFS๋ฅผ ๋๋ ค์ ํด๊ฒฐํ๋ค.
BOJ 1697 ์จ๋ฐ๊ผญ์ง
1. 0~ 100002 ์ฌ์ด์ dist๋ฐฐ์ด์ -1์ ๋ฃ์ด ์์ฑํ๋ค.
2. dist[N]์ ์ด๊ธฐ๊ฐ์ 0์ผ๋ก ๋ฃ์ด์ค๋ค. (์๋น์ด๊ฐ ์ถ๋ฐํ๋ ์๊ฐ ์ ์ฅ)
3. ๋์์ ์ฐพ์ ์๊ฐ์ด ๋ฃ์ด์ ธ์ -1์ด ์์ด์ง๋๊น์ง BFS๋ฅผ ๋๋ฆฐ๋ค.
import Foundation
let NK = readLine()!.split(separator: " ").map { Int($0)! }
let N = NK[0]
let K = NK[1]
var dist = Array(repeating: -1, count: 100002)
var idx = 0
var queue = [N]
dist[N] = 0
while dist[K] == -1 {
let cur = queue[idx]
idx += 1
let nxtArr = [cur-1, cur+1, cur*2]
for nxt in nxtArr {
if nxt<0 || nxt > 100000 { continue }
if dist[nxt] != -1 { continue }
dist[nxt] = dist[cur] + 1
queue.append(nxt)
}
}
print(dist[K])
๋ฐ์ํ
'๐ ์ฝํ > ์ ํ์ ๋ฆฌ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ถํ ์ ๋ณต] 2์ฐจ์๋ฐฐ์ด์ n์กฐ๊ฐ ๋ด๋ ๋ฌธ์ ์ ํ (0) | 2023.04.11 |
---|