给洛谷新手一个回答吧!!!
  • 板块学术版
  • 楼主Smart_Jason
  • 当前回复18
  • 已保存回复18
  • 发布时间2023/9/1 18:56
  • 上次更新2023/11/3 00:02:59
查看原帖
给洛谷新手一个回答吧!!!
767042
Smart_Jason楼主2023/9/1 18:56

为啥下载下来的样例在本地跑没事,一样输出,交到你谷就挂了啊!!!救救孩子。 程序:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,a,b,bz[200],cnt[1000000],ans[1000000],s[250];
int main(){
	scanf("%d%d%d",&n,&a,&b);
	for(int i=1;i<=n;i++) scanf("%d",&s[i]);
	bz[a]=1;
	cnt[0]=a;
	ans[0]=0;
	int i=-1,j=0;
	while(i<=j){
		i++;
		if(cnt[i]==b){
			printf("%d",ans[i]);
			return 0;
		}
		if(cnt[i]-s[cnt[i]]>=1&&bz[cnt[i]-s[cnt[i]]]==0){
			bz[cnt[i]-s[cnt[i]]]=1;
			j++;
			ans[j]=ans[i]+1;
			cnt[j]=cnt[i]-s[cnt[i]];
			if(cnt[j]==b){
			printf("%d",ans[j]);
			return 0;
		}
		}
		if(cnt[i]+s[cnt[i]]<=n&&bz[cnt[i]+s[cnt[i]]]==0){
			bz[cnt[i]+s[cnt[i]]]=1;
			j++;
			ans[j]=ans[i]+1;
			cnt[j]=cnt[i]+s[cnt[i]];
			if(cnt[j]==b){
			printf("%d",ans[j]);
			return 0;
		}
		}
	}
	printf("-1");
	return 0;
}

样例输入: 200 1 200

1 1 26 7 9 21 10 5 12 13 3 15 3 26 2 9 28 12 24 10 21 26 22 10 5 10 14 8 25 9 15 5 27 9 24 30 15 27 25 1 5 5 16 1 18 1 24 20 24 22 17 7 21 18 29 20 30 8 21 9 3 24 15 27 16 18 29 21 11 1 22 30 24 23 6 5 28 24 18 26 21 9 3 19 9 27 5 9 17 29 6 5 9 6 18 15 9 5 19 23 23 3 3 2 4 24 25 12 19 14 23 15 11 25 25 5 3 3 2 6 21 7 18 8 11 26 10 10 20 21 28 10 15 9 24 23 17 22 13 17 18 27 21 4 15 13 4 2 17 21 3 28 26 21 28 9 22 23 15 20 5 7 29 11 4 30 10 2 4 27 17 21 4 21 1 10 28 17 10 14 28 11 11 15 3 26 23 6 28 15 4 27 13 7 25 18 3 19 28 0

样例输出:17 题目链接

2023/9/1 18:56
加载中...