MLE,求调
  • 板块P1135 奇怪的电梯
  • 楼主UNNN
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/11/17 22:57
  • 上次更新2023/10/27 02:35:15
查看原帖
MLE,求调
780141
UNNN楼主2022/11/17 22:57
#include <iostream>
#include <cstdio>
using namespace std;
int n, a, b, ans = 0;
int k[255];
bool done = false;
void pressup(int &cur){
	if(cur + k[cur] <= n){
		cur = cur + k[cur];
	}
	return ;
}
void pressdown(int &cur){
	if(cur - k[cur] >= 1){
		cur = cur - k[cur];
	}
	return ;
}
void dfs(int flo){
	if(flo == b){
		done = true;
		return ;
	}
	
	if(flo + k[flo] <= n){
		pressup(flo);
		ans++;
		dfs(flo);
		if(!done){
			ans--;
		}
		return ;
	}
	if(flo - k[flo] >= 1){
		pressdown(flo);
		ans++;
		dfs(flo);
		if(!done){
			ans--;
		}
		return ;
	}
	return ;
}
int main()
{
	scanf("%d%d%d", &n, &a, &b);
	for(int i = 1;i <= n;i++){
		scanf("%d", &k[i]);
	}
	if(a == b){
		cout << "0";
		return 0;
	}
	dfs(a);
	if(done){
		printf("%d", ans);
	}
	else{
		printf("-1");
	}
	return 0;
}

2022/11/17 22:57
加载中...