bfs46pts求调(玄关一位)
查看原帖
bfs46pts求调(玄关一位)
1011587
Winalways楼主2024/7/26 10:04
#include <bits/stdc++.h>
using namespace std;
const int N=210;
int a1[N],vis[N];
int n,a,b;
int d[2]={-1,1};
struct state{
	int xy;
	int step;
};
void bfs(state st,int ed){
	queue<state> q;
	q.push(st);
	state now,next;
	vis[st.xy]=1;
	while(!q.empty()){
		now=q.front();
		int t=now.xy;
		if(now.xy==ed){
			
			cout<<now.step+1;
			
			return ;
		}
		if(now.xy+a1[now.xy]<=n&&vis[now.xy+a1[now.xy]]==0){
		
			next.xy=now.xy+a1[now.xy];
			vis[next.xy]=1;
			q.push(next);
			next.step++;
		}
		else if(vis[now.xy-a1[now.xy]]==0&&now.xy-a1[now.xy]>1){
			next.xy=now.xy-a1[now.xy];
			vis[next.xy]=1;
			q.push(next);
			next.step++;
			
		}
		q.pop();
	}
	cout<<"-1";
	return ;
}
int main(){
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++){
		cin>>a1[i];
	}
	state temp;
	temp.xy=a;
	temp.step=0;
	bfs(temp,b);
}
2024/7/26 10:04
加载中...