补全9月11日至13日课评:为13名学生创建请假文档,为5个班级创建总结文档
This commit is contained in:
55
C++代码/学习/奇怪的电梯.cpp
Normal file
55
C++代码/学习/奇怪的电梯.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include <bits/stdc++.h>
|
||||
#define endl '\n'
|
||||
using namespace std;
|
||||
|
||||
int n, a, b;
|
||||
int cnt = 0;
|
||||
int arr[100100];
|
||||
|
||||
int fx[] = {0, 1, -1};
|
||||
|
||||
int q[100000][2]; // ϱê0±íʾ´ÎÊý
|
||||
|
||||
void bfs(int k){
|
||||
int head = 1, tail = 1;
|
||||
q[head][0] = 0;
|
||||
q[head][1] = k;
|
||||
|
||||
while(head <= tail){
|
||||
int curr = q[head][1]; //µ±Ç°Â¥²ã
|
||||
|
||||
int tx;
|
||||
for(int i = 1; i <= 2; i++){
|
||||
tx = curr + fx[i] * arr[curr];
|
||||
|
||||
if(tx >= 1 && tx <= n){
|
||||
tail++;
|
||||
q[tail][0] = q[head][0] + 1;
|
||||
q[tail][1] = tx;
|
||||
|
||||
if(tx == b) {
|
||||
cout << q[tail][0];
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
head++;
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
cin >> n >> a >> b;
|
||||
if(a == b){
|
||||
cout << 0;
|
||||
return 0;
|
||||
}
|
||||
for(int i = 1; i <= n; i++){
|
||||
cin >> arr[i];
|
||||
}
|
||||
|
||||
bfs(a);
|
||||
|
||||
cout << -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user