'commit'
This commit is contained in:
43
TangDou/LanQiaoBei/LanQiaoBei13GuoSai/3_1.cpp
Normal file
43
TangDou/LanQiaoBei/LanQiaoBei13GuoSai/3_1.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <bits/stdc++.h>
|
||||
/*
|
||||
5
|
||||
2 1 5 1 3
|
||||
|
||||
答案 2
|
||||
*/
|
||||
using namespace std;
|
||||
struct Node {
|
||||
int pos;
|
||||
int step;
|
||||
};
|
||||
const int N = 110;
|
||||
int a[N];
|
||||
int st[N];
|
||||
int n;
|
||||
void bfs() {
|
||||
queue<Node> q;
|
||||
q.push({1, 0});
|
||||
st[1] = 1;
|
||||
while (q.size()) {
|
||||
auto t = q.front();
|
||||
q.pop();
|
||||
for (int i = 1; i <= a[t.pos]; i++) {
|
||||
int x = t.pos + i;
|
||||
// 先判断下一跳是不是终点
|
||||
if (x == n) {
|
||||
cout << t.step + 1 << endl;
|
||||
exit(0);
|
||||
}
|
||||
// 正常入队列
|
||||
if (st[x]) continue;
|
||||
q.push({x, t.step + 1});
|
||||
st[x] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
cin >> n;
|
||||
for (int i = 1; i <= n; i++) cin >> a[i];
|
||||
bfs();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user