未解之谜 令人心痛

奇怪的电梯

AC代码

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int counter = 99999;
int n, A, B;
int k[201], book[201];
int c[201];

void dfs(int a, int sum) {
	if (a == B) {
		counter = min(sum, counter);
		return ;
	}
	if (sum > counter) {
		return ;
	}
	book[a] = 1;
	if (a + k[a] <= n && book[a + k[a]] == 0) {
		dfs(a + k[a], sum + 1);
	}
	if (a - k[a] >= 1 && book[a - k[a]] == 0) {
		dfs(a - k[a], sum + 1);
	}
	book[a] = 0;
}


int main() {
	cin >> n >> A >> B;
	for (int i = 1; i <= n; i++) {
		cin >> k[i];
	}
	dfs(A, 0);
	if (counter == 99999)
		cout << "-1";
	else
		cout << counter;
	return 0;
}

80分版本

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int counter = 99999;
int sum = 0;
int n, A, B;
int k[201], book[201];

void dfs(int a) {
	if (a == B) {
		counter = min(sum, counter);
		return ;
	}
	if (sum > counter) {
		return ;
	}
	book[a] = 1;
	if (a + k[a] <= n && book[a + k[a]] == 0) {
		sum++;
		dfs(a + k[a]);
	}
	if (a - k[a] >= 1 && book[a - k[a]] == 0) {
		sum++;
		dfs(a - k[a]);
	}
	book[a] = 0;
}


int main() {
	cin >> n >> A >> B;
	for (int i = 1; i <= n; i++) {
		cin >> k[i];
	}
	dfs(A);
	if (counter == 99999)
		cout << "-1";
	else
		cout << counter;
	return 0;
}

WA测试数据:
输入:
150 1 150
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 1 0
输出:
14

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值