求助
  • 板块灌水区
  • 楼主_xiaofu_
  • 当前回复1
  • 已保存回复1
  • 发布时间2025/1/8 09:34
  • 上次更新2025/1/8 18:42:17
查看原帖
求助
789769
_xiaofu_楼主2025/1/8 09:34

内存4个G(笑死),怎么优化?(BFS)

P1135 奇怪的电梯

#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;
}

2025/1/8 09:34
加载中...