社区讨论
求
P1135奇怪的电梯参与者 2已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @m3uc5bw9
- 此快照首次捕获于
- 2024/11/23 23:39 去年
- 此快照最后确认于
- 2025/11/04 14:04 4 个月前
为什么会爆内存呢
CPP#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
struct fl{
int floor,d;
};
queue<fl> q;
int n,a,b;
int k[maxn],vis[maxn];
int main(){
cin>>n>>a>>b;
for(int i=1;i<=n;i++){
cin>>k[i];
}
q.push((fl){a,0});
vis[a]=1;
fl now;
while(!q.empty()){
now=q.front();
q.pop();
if(now.floor==b){
break;
}
for(int i=-1;i<=1;i+=2){
int ft=now.floor+k[now.floor]*i;
if(ft>=1&&ft<=n&&vis[ft]==0){
q.push((fl){ft,now.d+1});
vis[ft]==1;
}
}
}
if(now.floor==b){
cout<<now.d;
}
else {
cout<<-1<<"\n";
}
return 0;
}
回复
共 2 条回复,欢迎继续交流。
正在加载回复...