Search
Duplicate
📗

점프 점프

주차
문제번호
14248
언어
C++
티어
실버
유형
그래프
BFS
DFS
nj_Blog
nj_상태
이해도
100%
풀이
사람
이해도 2
13 more properties

문제접근

전형적인 dfs 문제

놓쳤던 부분

코드

2528 KB

8 ms

#include <iostream> #include <vector> using namespace std; int n; int s; int answer = 0;; vector<int> stones; vector<bool> visited; void dfs(int currentStone) { if (visited[currentStone]) return ; answer++; visited[currentStone] = true; int nextLeftStone = currentStone - stones[currentStone]; int nextRightStone = currentStone + stones[currentStone]; if (nextLeftStone >= 1) dfs(nextLeftStone); if (nextRightStone <= n) dfs(nextRightStone); } int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; stones.resize(n + 1); visited.resize(n + 1); for (int i = 1; i <= n; i++) cin >> stones[i]; cin >> s; dfs(s); cout << answer; return (0); }
C++
복사