爸爸的雷达改妈妈的达雷1

yang_zhiyu 2023-11-25 10:17:16

#include<bits/stdc++.h>
using namespace std;

const int N=100000+10;
const int dx[]={0,0,-1,1};
const int dy[]={-1,1,0,0};

int d[N];
int dep[N];
queue<int> que;

int main()
{
	int n,m;
	cin>>n;
	
	for(int i=1;i<=n;i++)
	{
		cin>>d[i];
	}
	memset(dep,-1,sizeof(dep));
	dep[n]=0;
	que.push(n);
	while(!que.empty())
	{
		int x=que.front(),y=que.front();
		que.pop();
	
		int nx=x+d[x];
		if(nx<=n&&dep[nx]==-1)
		{
			dep[nx]=dep[x]+1;
			que.push(nx) ;
		} 
		nx=x-d[x];
		if(nx>=1&&dep[nx]==-1)
		{
			dep[nx]=dep[x]+1;
			que.push(nx);
		}
	}
	cout<<dep[1]<<endl;
	return 0;
} 

共 2 条回复

yangrh

爸爸的雷达

RRRRRRrrrrrrr