088李宥锦 - 周赛8.C - P1135 奇怪的电梯
public
Apr 26, 2025
Never
35
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,a,b; 4 int k[205]; 5 struct node 6 { 7 int h; 8 int step; 9 }; 10 node s; 11 int vis[205]; 12 int dir[2]={1,-1}; 13 queue<node> q; 14 void bfs() 15 { 16 s.h=a; 17 s.step=0; 18 vis[s.h]=1; 19 q.push(s); 20 while(!q.empty()) 21 { 22 node tmp=q.front(); 23 q.pop(); 24 if(tmp.h==b) 25 { 26 cout<<tmp.step; 27 return; 28 } 29 vis[tmp.h]=1; 30 for(int i=0;i<2;i++) 31 { 32 node a; 33 a.h=tmp.h+k[tmp.h]*dir[i]; 34 a.step=tmp.step+1; 35 if(a.h<=n&&a.h>0&&!vis[a.h]) 36 { 37 vis[a.h] = true; // 64分,标记访问过 就 AC了 38 q.push(a); 39 } 40 } 41 } 42 cout<<-1; 43 } 44 int main() 45 { 46 cin>>n>>a>>b; 47 for(int i=1;i<=n;i++) 48 { 49 cin>>k[i]; 50 } 51 bfs(); 52 return 0; 53 }