蒟蒻代码求调QAQ,50pts #6#7#9#10#11WA
查看原帖
蒟蒻代码求调QAQ,50pts #6#7#9#10#11WA
564921
UniPengu楼主2023/9/1 22:18
#include <bits/stdc++.h>
#define rep(i,n) for(int i=1;i<=n;i++)
#define rep0(i,n) for(int i=0;i<n;i++)
#define int long long
using namespace std;
int n,start,ed,ans=1;
int a[205];
bool vis[205];
queue <int> q;
void bfs(){
	int x,y;
	while(!q.empty()) q.pop();
	memset(vis,0,sizeof(vis));
	q.push(start);
	vis[start]=1;
	while(!q.empty()){
		x=0;y=0;
		int tmp=q.front();
		q.pop();
		if(tmp+a[tmp]>=1 && tmp+a[tmp]<=n && !vis[tmp+a[tmp]]){
			x=tmp+a[tmp];
			if(x==ed) {
				break;
			}
			else {
				ans++;
				q.push(x);
				vis[x]=1;
			}
		}
		if(tmp-a[tmp]>=1 && tmp-a[tmp]<=n && !vis[tmp-a[tmp]]){
			y=tmp-a[tmp];
			if(y==ed) {
				break;
			}
			else {
				ans++;
				q.push(y);
				vis[y]=1;
			}
		}
	}
		if(x==ed || y==ed) cout<<ans<<endl;
		else cout<<"-1"<<endl;
		return ;	
}
signed main()
{	
	cin>>n>>start>>ed;
	rep(i,n) cin>>a[i];
	if(start==ed) cout<<"0"<<endl;
	else {
		bfs();
	}
	
	
 	return 0;
}
2023/9/1 22:18
加载中...