我服了()
查看原帖
我服了()
955954
Miracle_InDream楼主2024/1/3 18:14
#include<bits/stdc++.h>
using namespace std;
int n,a,b;
int k[205];
struct node
{
	int h;
	int step;
};
node s;
int vis[205];
int dir[2]={1,-1};
queue<node> q;
void bfs()
{
	s.h=a;
	s.step=0;
	vis[s.h]=1;
	q.push(s);
	while(!q.empty())
	{
		node tmp=q.front();
		q.pop();
		if(tmp.h==b)
		{
			cout<<tmp.step;
			return;
		}
		if(vis[tmp.h])
		{
			continue;
		}
		vis[tmp.h]=1;
		for(int i=0;i<2;i++)
		{
			node a;
			a.h=tmp.h+k[tmp.h]*dir[i];
			if(a.h<=n&&a.h>0&&!vis[a.h])
			{
				q.push(a);
			}
		}
	}
	cout<<-1;
}
int main()
{
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++)
	{
		cin>>k[i];
	}
	bfs();
	return 0;
}

输入样例送我个−1-1()

2024/1/3 18:14
加载中...