diff --git "a/alstjr7437/BFS/\352\262\214\354\236\204-\353\247\265-\354\265\234\353\213\250\352\261\260\353\246\254.py" "b/alstjr7437/BFS/\352\262\214\354\236\204-\353\247\265-\354\265\234\353\213\250\352\261\260\353\246\254.py" index c5d22a81..17b42006 100644 --- "a/alstjr7437/BFS/\352\262\214\354\236\204-\353\247\265-\354\265\234\353\213\250\352\261\260\353\246\254.py" +++ "b/alstjr7437/BFS/\352\262\214\354\236\204-\353\247\265-\354\265\234\353\213\250\352\261\260\353\246\254.py" @@ -1,51 +1,30 @@ -import Foundation - -func solution(_ maps: [[Int]]) -> Int { - var maps = maps - let dx = [-1, 1, 0, 0] - let dy = [0, 0, -1, 1] - let rowCount = maps.count - let colCount = maps[0].count - - func bfs(_ x: Int, _ y: Int) -> Int { - var queue = [(x, y)] - var index = 0 - - while index < queue.count { - let (x, y) = queue[index] - index += 1 - - for i in 0..<4 { - let nx = x + dx[i] - let ny = y + dy[i] - - if nx < 0 || nx >= rowCount || ny < 0 || ny >= colCount { - continue - } - - if maps[nx][ny] == 0 { - continue - } - - if maps[nx][ny] == 1 { +from collections import deque +def solution(maps): + answer = 0 + + dx = [-1, 1, 0, 0] + dy = [0, 0, -1, 1] + + def bfs(x, y): + queue = deque() + queue.append((x, y)) + + while queue: + x, y = queue.popleft() + + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + + if nx < 0 or nx >= len(maps) or ny < 0 or ny >= len(maps[0]): continue + + if maps[nx][ny] == 0: continue + + if maps[nx][ny] == 1: maps[nx][ny] = maps[x][y] + 1 queue.append((nx, ny)) - } - } - } - - return maps[rowCount - 1][colCount - 1] - } - - let answer = bfs(0, 0) - return answer == 1 ? -1 : answer -} - -// 예시 호출 -let maps = [ - [1, 0, 1, 1, 1], - [1, 0, 1, 0, 1], - [1, 1, 1, 0, 1], - [0, 0, 0, 0, 1] -] -print(solution(maps)) // 출력: 11 \ No newline at end of file + + return maps[len(maps)-1][len(maps[0])-1] + + answer = bfs(0, 0) + return -1 if answer == 1 else answer \ No newline at end of file