why?
查看原帖
why?
719843
lizhuorong楼主2024/4/6 13:05

前六个MLE(。・∀・)ノ

#include<bits/stdc++.h>
using namespace std;
queue<int>q;
bool g[205];
int a[205];
int n,A,b;
void bfs(int i){
	int x,y;
	q.push(i);
	q.push(0);
	while(!q.empty()){
		x=q.front();
		q.pop(); 
		g[x]=1;
		y=q.front();
		q.pop();
		if(x==b){
		    cout<<y;
		    return ;
        }
		if(x+a[x]<=n&&g[x+a[x]]==0){
			q.push(x+a[x]);
			q.push(y+1);
		}
		if(x-a[x]>=1&&g[x-a[x]]==0){
			q.push(x-a[x]);
			q.push(y+1);
		}
	}
	cout<<-1<<endl;
	return ;
}
int main(){
	cin>>n>>A>>b;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	bfs(A);
}
2024/4/6 13:05
加载中...