알고리즘
백준 11060번 점프 점프 C++
영춘권의달인
2022. 11. 7. 15:59
https://www.acmicpc.net/problem/11060
11060번: 점프 점프
재환이가 1×N 크기의 미로에 갇혀있다. 미로는 1×1 크기의 칸으로 이루어져 있고, 각 칸에는 정수가 하나 쓰여 있다. i번째 칸에 쓰여 있는 수를 Ai라고 했을 때, 재환이는 Ai이하만큼 오른쪽으로
www.acmicpc.net
bfs
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int n;
vector<int> v;
vector<bool> found;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
v = vector<int>(n);
found = vector<bool>(n, false);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
queue<pair<int, int>> q;
q.push({ 0,0 });
found[0] = true;
int ans = -1;
while (!q.empty()) {
auto curr = q.front();
q.pop();
if (curr.first == v.size() - 1) {
ans = curr.second;
break;
}
for (int i = 1; i <= v[curr.first]; i++) {
int next = curr.first + i;
if (next >= v.size()) break;
if (found[next]) continue;
q.push({ next,curr.second + 1 });
found[next] = true;
}
}
cout << ans;
}