萌新求助
查看原帖
萌新求助
418544
化尘star楼主2021/9/12 14:37

请教下,我搜索内部的,return为什么>=不行, 而>行

#include<iostream>
using namespace std;
int N,A,B,a[1000],c[1000];
void work(int x,int y)
{
    if(y<c[x])
	c[x]=y;
	if(y>c[x])return;//就是这里
	if(x+a[x]<=N)work(x+a[x],y+1);
	if(x-a[x]>0)work(x-a[x],y+1);
	return;
}
int main()
{
	cin>>N>>A>>B;
	for(int i=1;i<=N;i++)
	{
		cin>>a[i];
		c[i]=1000;
	}
	work(A,0);
	if(c[B]==1000)
	cout<<-1<<endl;
	else
	cout<<c[B];
	return 0;
}
2021/9/12 14:37
加载中...