洛谷P1135 奇怪的电梯

题目来源:https://www.luogu.com.cn/problem/P1135
`
typedef pair<int, int>Pi;
int N, A, B;bool hs[201];
int K[250];queueq;

int bfs(int nown, int fn) {
q.push({ nown,0 });
hs[nown] = 1;
while (!q.empty()) {
Pi pun = q.front();
q.pop();
if (pun.first == B) {
return pun.second;
}
Pi uppero, lowero;
uppero.first = pun.first + K[pun.first];
if (uppero.first >= 1 && uppero.first <= N && !hs[uppero.first]) {
uppero.second = pun.second + 1;hs[uppero.first] = 1;
q.push(uppero);
}
lowero.first = pun.first - K[pun.first];
if (lowero.first >= 1 && lowero.first <= N && !hs[lowero.first]) {
lowero.second = pun.second + 1;hs[lowero.first] = 1;
q.push(lowero);
}
}
return -1;
}

int main() {
cin >> N >> A >> B;
for (int i = 1;i <= N;i++) {
cin >> K[i];
}
int res = bfs(A, B);
cout << res << endl;
return 0;
}`
第一次交的时候30分,基本上没过都是MLE,尝试剪枝(使用数组hs[]存储走过的格子),AC了。

posted @ 2025-04-28 19:17  yubai111  阅读(12)  评论(0)    收藏  举报