为什么会RE啊,萌新求助!!!
查看原帖
为什么会RE啊,萌新求助!!!
533209
wyz_ul楼主2021/11/19 20:29
#include<iostream>
#include<cstdio>
using namespace std;
int k[300], st, fs, ans = 9999999,n;
bool book[300];
void dfs(int x, int num)
{
	if (x == fs)
	{
		ans = min(ans, num);
		return;
	}
	if (num > ans)
		return;
	for (int i = -1; i <= 1; i += 2)
	{
		int xx = x + i * k[x];
		if (xx >= 1 && xx <= n)
		{
			if (book[xx] == 0)
			{
				book[xx] = 1;
				dfs(xx, num + 1);
				book[xx] = 0;
			}
		}
	}

}
int main()
{
	scanf("%d%d%d", &n, &st, &fs);
	for (int i = 1; i <= n; i++)
		scanf("%d", &k[i]);
	dfs(st, 0);
	if (ans == 9999999)
		printf("-1");
	else printf("%d", ans);
	return 0;
}
2021/11/19 20:29
加载中...