Algorithm
[Swift 백준] 14248 점프 점프
Kim Roks
2025. 3. 6. 17:50
https://www.acmicpc.net/problem/14248
풀이 아이디어
이차원 그래프를 이용한 그래프탐색 문제입니다
이동방향은 좌 우 모두 가능하므로 [-1, 1] 로 지정합니다.
풀이
let n = Int(readLine()!)!
let graph = readLine()!.split(separator: " ").map { Int($0)! }
//인덱스 맞추기
let s = Int(readLine()!)! - 1
var isvisited = Array(repeating: false, count: n)
let direction = [-1, 1]
func dfs(position: Int) {
if position < 0 || position >= n || isvisited[position] == true {
return
}
isvisited[position] = true
let jump = graph[position]
for dir in direction {
let next = position + jump * dir
dfs(position: next)
}
}
dfs(position: s)
print(isvisited.filter { $0 == true }.count)