PS

BOJ 11060 : 점프 점프

lickelon 2025. 1. 22. 21:57

코드

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()

#define INF 987654321

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n;
    cin >> n;
    vector<int> arr(n);
    for(auto &e : arr) cin >> e;
    
    vector<int> dp(n, INF);
    dp[0] = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 1; j <= arr[i]; j++) {
            if(i+j >= n) break;
            dp[i+j] = min(dp[i+j], dp[i] + 1);
        } 
    }
    cout << (dp[n-1] == INF ? -1 : dp[n-1]);

    return 0;
}

풀이

1차원 DP이다.

각 칸에서 갈 수 있는 칸의 값을 업데이트 해주면 된다.

728x90

'PS' 카테고리의 다른 글

BOJ 2823 : 유턴 싫어  (0) 2025.01.24
BOJ 10025 : 게으른 백곰  (0) 2025.01.23
BOJ 10866 : 덱  (0) 2025.01.21
BOJ 29160 : 나의 FIFA 팀 가치는?  (0) 2025.01.20
BOJ 14940 : 쉬운 최단거리  (0) 2025.01.19