From 0d211e032d373987387522428c417653eadec279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=84=9D=7CMinseok=20Kim?= Date: Tue, 6 Aug 2024 23:12:30 +0900 Subject: [PATCH] 31-alstjr7437 (#218) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 31차시 solved * swift 코드 추가 --- ...34\353\213\250\352\261\260\353\246\254.py" | 51 +++++++++++++++++++ ...353\213\250\352\261\260\353\246\254.swift" | 47 +++++++++++++++++ alstjr7437/README.md | 3 +- 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 "alstjr7437/BFS/\352\262\214\354\236\204-\353\247\265-\354\265\234\353\213\250\352\261\260\353\246\254.py" create mode 100644 "alstjr7437/BFS/\352\262\214\354\236\204-\353\247\265-\354\265\234\353\213\250\352\261\260\353\246\254.swift" 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" new file mode 100644 index 00000000..c5d22a81 --- /dev/null +++ "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" @@ -0,0 +1,51 @@ +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 { + 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 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.swift" "b/alstjr7437/BFS/\352\262\214\354\236\204-\353\247\265-\354\265\234\353\213\250\352\261\260\353\246\254.swift" new file mode 100644 index 00000000..cfe50d2c --- /dev/null +++ "b/alstjr7437/BFS/\352\262\214\354\236\204-\353\247\265-\354\265\234\353\213\250\352\261\260\353\246\254.swift" @@ -0,0 +1,47 @@ +import Foundation + +func solution(_ maps: inout [[Int]]) -> Int { + 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 { + 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 +} + +// 예시 호출 +var maps = [ + [1, 0, 1, 1, 1], + [1, 0, 1, 0, 1], + [1, 0, 1, 1, 1], + [1, 1, 1, 0, 1], + [0, 0, 0, 0, 1] +] +print(solution(&maps)) // 출력: 11 \ No newline at end of file diff --git a/alstjr7437/README.md b/alstjr7437/README.md index 7dcd819b..2c0cbd2d 100644 --- a/alstjr7437/README.md +++ b/alstjr7437/README.md @@ -31,4 +31,5 @@ | 27차시 | 2024.05.26 | 우선순위 큐 | 이중 우선순위 큐 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/198 | | 28차시 | 2024.05.30 | 브루트 포스 | 카잉 달력 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/203 | | 29차시 | 2024.06.11 | 이분 탐색 | 나무 자르기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/210 | -| 30차시 | 2024.06.19 | 방 번호 | 구현 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/211 | \ No newline at end of file +| 30차시 | 2024.06.19 | 방 번호 | 구현 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/211 | +| 31차시 | 2024.06.19 | 게임 맵 최단거리 | BFS | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/218 | \ No newline at end of file