100分了,但TLE
查看原帖
100分了,但TLE
1048717
sunnytutu楼主2025/1/19 14:48

各位大佬行行好,帮帮忙吧!

特判无效! 特判无效! 特判无效!

#include<bits/stdc++.h>
using namespace std;
int n, a, b, ans = 0xfffff;
int to[205];
bool vis[205];
void dfs(int now, int sum) {
	if (now == b) {
		ans = min(ans, sum);
	}
	if (sum > ans) {
		return;
	}
	vis[now] = 1;
	if (now + to[now] <= n && !vis[now + to[now]]) {
		dfs(now + to[now], sum + 1);
	}
	if (now - to[now] >= 1 && !vis[now - to[now]]) {
		dfs(now - to[now], sum + 1);
	}
	vis[now] = 0;
}
int main() {
	scanf("%d%d%d",&n,&a,&b);
	for(int i=1;i<=n;i++){
		cin>>to[i];
	}
	vis[a] = 1;
	dfs(a,0);
	if (ans != 0xfffff) {
		printf("%d",ans);
	} else {
		printf("-1");
	}
	return 0;
}

提交记录传送门

在线等

2025/1/19 14:48
加载中...