内存4个G(笑死),怎么优化?(BFS)
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int k[201]={0},n,a,b;
struct lift{
int now;
int step;
};
int bfs(){
queue<lift> que;
lift s;
s.now=a;
s.step=0;
que.push(s);
while(!que.empty()){
lift t,t1,t2;
t=que.front();
if(t.now==b){
return t.step;
}
t1=t;t2=t;
t1.now+=k[t.now];
t2.now-=k[t.now];
t1.step++;
t2.step++;
que.pop();
if(t1.now<=n&&t1.now>0){
que.push(t1);
}
if(t2.now<=n&&t2.now>0){
que.push(t2);
}
}
return -1;
}
int main(){
cin>>n>>a>>b;
for(int i=1;i<=n;i++){
cin>>k[i];
}
cout<<bfs()<<endl;
return 0;
}