본문 바로가기
Algorithm/문제풀이_백준

[Swift][BFS] 백준 17086번 (아기상어2)

by Joahnee 2022. 1. 28.

요구능력 : BFS

 

코드설명 : 

 

 

후기 : 기초적인 BFS문제인것 같다.

아기상어보다 아기상어2 난이도가 훨씬낮다.

let nm = readLine()!.split(separator: " ").map{Int(String($0))!}
let n = nm[0]
let m = nm[1]
var arr = [[Int]]()
let dx = [-1, 1, 0, 0, -1, 1, 1, -1]
let dy = [0, 0, -1, 1, 1, -1, 1, -1]
for _ in 0..<n{
    arr.append(readLine()!.split(separator: " ").map{Int(String($0))!})
}

var result = [Int]()
func bfs(_ startX: Int, _ startY: Int){
    var visited = Array(repeating: Array(repeating: false, count: 251), count: 251)
    var queue = [((Int, Int), Int)]() //x, y, 안전거리
    
    queue.append(((startX, startY), 0))
    visited[startX][startY] = true
    var idx = 0
    while queue.count > idx {
        let pop = queue[idx]
        let x = pop.0.0
        let y = pop.0.1
        let safeDistance = pop.1
        idx += 1
        if arr[x][y] == 1 {
            result.append(safeDistance)
            break
        }
        for i in 0..<8{
            let nx = x + dx[i]
            let ny = y + dy[i]
            
            if nx < 0 || nx >= n || ny < 0 || ny >= m || visited[nx][ny] {continue}
            queue.append(((nx, ny), safeDistance + 1))
            visited[nx][ny] = true
        }
        
        
    }
}
for i in 0..<n {
    for j in 0..<m{
        if arr[i][j] == 1 {continue}
        bfs(i, j)
    }
}
print(result.max()!)

댓글