BFS36分求解,全WA

P1135 奇怪的电梯

能解释下这个吗 @[petezhuang](/user/1280303) ``` for(int i = -1; i < 2;i += 2) ```
by dzsf_lhz @ 2024-03-16 08:13:56


@[dzsf_lhz](/user/946954) 就是一个乘1一个乘-1,一个上升一个下降
by petezhuang @ 2024-03-17 17:26:19


参考下吧,我确实太菜了 ``` #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-17 18:14:22


|