题目描述:
思路:
见代码注释
AC代码:
#include <bits/stdc++.h>
using namespace std;
int n, A, B;
int t[250]; //记录到达每层所用的最短时间
int a[250];
void dfs(int lou, int sum) {
//lou是当前的楼层数,sum为当前按键次数
t[lou] = sum; //先进行赋值
//下
if (lou - a[lou] > 0 && sum + 1 < t[lou-a[lou]]) {
dfs(lou - a[lou], sum + 1);
}
//上
if (n >= a[lou] + lou && sum + 1 < t[lou+a[lou]]) {
dfs(lou + a[lou], sum + 1);
}
}
int main() {
std::ios::sync_with_stdio(false);
//减少cin和cout的用时
memset(t, 0x7f7f7f7f, sizeof(t));
//0x7f7f7f7f为int型最大值
cin >> n >> A >> B;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
dfs(A, 0);
//若t[B]的值没有改变即从未到达B楼层
if (t[B] == 0x7f7f7f7f) {
cout << -1;
return 0;
}
cout << t[B];
return 0;
}
标签:cout,int,sum,dfs,电梯,lou,0x7f7f7f7f,奇怪
From: https://blog.csdn.net/whq060212/article/details/137613465