c++ 广搜 80分 9,10错了
查看原帖
c++ 广搜 80分 9,10错了
632830
Mark_666楼主2023/5/7 20:07
#include<bits/stdc++.h>
using namespace std;
int N,A,B; 
int a[300];
int b[300];
struct Node
{
	int i;
	int step;
}q[10000];
void bfs()
{
	int h=1,t=1;
	q[h].i=A;
	while(h<=t)
	{
		Node tmp=q[h++];
		if(tmp.i==B)
		{
			cout<<tmp.step;
			exit(0);
		}
		int x=tmp.i+a[tmp.i];
		int y=tmp.i-a[tmp.i];
		if(x>=1&&x<=N&&b[tmp.i]==0)
		{
			b[tmp.i]=1;
			q[++t]=(Node){x,tmp.step+1};
		}
			
		if(y>=1&&y<=N&&b[tmp.i]==0)
		{
			b[tmp.i]=1;
			q[++t]=(Node){y,tmp.step+1};
		}
			
	}
}
int main()
{
    cin>>N>>A>>B;
    for(int i=1;i<=N;i++)
    	cin>>a[i];
    bfs();
    cout<<-1;
    return 0;
}
2023/5/7 20:07
加载中...