40分,蒟蒻求助!!!

P1135 奇怪的电梯

~~我太菜了,所以只能打个BFS给你~~ ``` #include<bits/stdc++.h> using namespace std; bool p[200005]; // 起始 终止 int newx, n, s, t, a[200005]; struct dian{ int ans, temp; }; bool check(int x){ return x <= n && x > 0 && !p[x]; } int bfs(int x){ queue <dian> q; dian f; p[x] = true; q.push({x, 0}); while(!q.empty()){ f = q.front(), q.pop(); if(f.ans == t) return f.temp; // printf("%d %d\n", f.temp, f.ans); if(check(f.ans + a[f.ans])) q.push({f.ans + a[f.ans], f.temp + 1}), p[f.ans + a[f.ans]] = true; if(check(f.ans - a[f.ans])) q.push({f.ans - a[f.ans], f.temp + 1}), p[f.ans - a[f.ans]] = true; } return -1; } int main(){ scanf("%d %d %d", &n, &s, &t); for(int i = 1;i <= n;i ++) scanf("%d", &a[i]); printf("%d", bfs(s)); return 0; } ```
by dzsf_lhz @ 2024-03-16 08:15:51


|