Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- paragraph style
- Tuist
- swift 백준
- swift concurrency
- domain data
- swift 점선
- swift dashed line
- 타임라인 포맷팅
- swift custom ui
- rxdatasources
- coordinator
- task cancellation
- custom navigation bar
- custom ui
- task cancel
- BFS
- swift bottomsheet
- claen architecture
- button configuration
- SWIFT
- custombottomsheet
- identifiable
- uikit toast
- traits
- 버튼 피드백
- UIKit
- DP
- swift navigationcontroller
- RxSwift
- reactorkit
Archives
- Today
- Total
김경록의 앱 개발 여정
[Swift 백준] 16173 점프왕 쩰리 (Small) 본문
https://www.acmicpc.net/problem/16173

풀이 아이디어
그래프 탐색 문제입니다
bfs를 선호하지만 연습을 위해 재귀를 활용한 dfs로 풀이했습니다.
두 방향으로만 이동가능하며 그래프를 넘어가면 즉시 종료시켰습니다.
풀이
import Foundation
let n = Int(readLine()!)!
var graph = [[Int]]()
for _ in 0..<n {
graph.append(readLine()!.split(separator: " ").map { Int($0)! })
}
var visited = Array(repeating: Array(repeating: false, count: n), count: n)
let dx = [0, 1]
let dy = [1, 0]
func dfs(_ x: Int, _ y: Int) -> Bool {
if x < 0 || y < 0 || x >= n || y >= n || visited[x][y] {
return false
}
if graph[x][y] == -1 {
return true
}
visited[x][y] = true
let jump = graph[x][y]
for i in 0..<2 {
let nx = x + dx[i] * jump
let ny = y + dy[i] * jump
if dfs(nx, ny) {
return true
}
}
return false
}
print(dfs(0, 0) ? "HaruHaru" : "Hing")
'Algorithm' 카테고리의 다른 글
[Swift 백준] 1927 최소 힙 (0) | 2025.03.14 |
---|---|
[Swift 백준] 14248 점프 점프 (0) | 2025.03.06 |
[Swift 백준] 24480 알고리즘 수업 - 깊이 우선 탐색 2 (0) | 2025.02.26 |
[Swift 백준] 7785 회사에 있는 사람 (0) | 2025.02.20 |
[Swift 백준] 5567 결혼식 (0) | 2025.02.19 |