返回题解分享
讨论 / 题解分享/ 帖子详情

奇怪的电梯 - 题解

#include <iostream>
#include <queue>
#include <cstring>
#define x first
#define y second
using namespace std;
const int N=210;
int g[N];
int st[N];
typedef pair<int,int>PII;
int n;

void bfs(int start,int end)
{
	queue<PII>q;
	q.push({start,g[start]});
	st[start]=0;
	
	while(q.size())
	{
		auto t=q.front();
		q.pop();
		
		int a=t.x-g[t.x],b=t.x+g[t.x];
		if(a>=1&&st[a]==-1){
			q.push({a,g[a]});
			st[a]=st[t.x]+1;
		}
		
		if(b<=n&&st[b]==-1){
			q.push({b,g[b]});
			st[b]=st[t.x]+1;
		}	
	}
}

int main()
{
	int a,b;
	cin>>n>>a>>b;
	
	for(int i=1;i<=n;i++)	cin>>g[i];
	
	memset(st,-1,sizeof st);
	bfs(a,b);
	
	cout<<st[b];
	return 0;
}
0 回复 0 转发 0 喜欢 4 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!