求助90分代码 样例输出2 实在检查不出来
查看原帖
求助90分代码 样例输出2 实在检查不出来
472551
zzujhh楼主2021/2/21 10:38
#include<bits/stdc++.h>
using namespace std;

int n,a,b;
int min1;
int f[201];
int flag;
bool vis[201];
int e[2]= {1,-1};
struct st{
	int x,y;
}floor1,floor2;
queue<st>q;

void bfs(int a,int b) {
	f[a]=true;
	st tmp;
	tmp.x=a;
	tmp.y=0;
	q.push(tmp);
	while(!q.empty()) {
		floor1=q.front();
		q.pop();
		if(floor1.x==b) {
			flag=1;
			return;
		}
		for(int i=0; i<2; i++) {
			floor2.x=floor1.x+f[floor1.x]*e[i];
			if(vis[floor2.x]==true)continue;
			if(floor2.x>0&&floor2.x<=n) {
				vis[floor2.x]=true;
				floor2.y=floor1.y+1;
				q.push(floor2);
			}
		}
	}
}

int main() {
	cin>>n>>a>>b;
	for(int i=1; i<=n; i++)cin>>f[i];
	bfs(a,b);
	if(flag==1)cout<<floor1.y;
	else cout<<-1;
	return 0;
}
2021/2/21 10:38
加载中...