Skip to content

Commit

Permalink
31-alstjr7437 (#218)
Browse files Browse the repository at this point in the history
* 31์ฐจ์‹œ solved

* swift ์ฝ”๋“œ ์ถ”๊ฐ€
  • Loading branch information
alstjr7437 authored Aug 6, 2024
1 parent 243234a commit 0d211e0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion alstjr7437/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@
| 27์ฐจ์‹œ | 2024.05.26 | ์šฐ์„ ์ˆœ์œ„ ํ | <a href="https://www.acmicpc.net/problem/7662">์ด์ค‘ ์šฐ์„ ์ˆœ์œ„ ํ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/198 |
| 28์ฐจ์‹œ | 2024.05.30 | ๋ธŒ๋ฃจํŠธ ํฌ์Šค | <a href="https://www.acmicpc.net/problem/6064">์นด์ž‰ ๋‹ฌ๋ ฅ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/203 |
| 29์ฐจ์‹œ | 2024.06.11 | ์ด๋ถ„ ํƒ์ƒ‰ | <a href="https://www.acmicpc.net/problem/2805">๋‚˜๋ฌด ์ž๋ฅด๊ธฐ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/210 |
| 30์ฐจ์‹œ | 2024.06.19 | ๋ฐฉ ๋ฒˆํ˜ธ | <a href="https://www.acmicpc.net/problem/1475">๊ตฌํ˜„</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/211 |
| 30์ฐจ์‹œ | 2024.06.19 | ๋ฐฉ ๋ฒˆํ˜ธ | <a href="https://www.acmicpc.net/problem/1475">๊ตฌํ˜„</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/211 |
| 31์ฐจ์‹œ | 2024.06.19 | ๊ฒŒ์ž„ ๋งต ์ตœ๋‹จ๊ฑฐ๋ฆฌ | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/1844">BFS</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/218 |

0 comments on commit 0d211e0

Please sign in to comment.