题意:
电梯,可以往上走或往下走,求从A到B楼的最短时间。
解析:
一维bfs,水。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1
using namespace std;
const int maxn = 200 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double ee = exp(1.0);
int n, fr, to;
int step[maxn];
int dp[maxn];
int bfs()
{
memset(dp, -1, sizeof(dp));
queue<int> q;
q.push(fr);
dp[fr] = 0;
while (!q.empty())
{
int now = q.front();
q.pop();
if (now == to)
return dp[now];
int next = now + step[now];
if (next <= n)
{
if (dp[next] == -1)
{
dp[next] = dp[now] + 1;
q.push(next);
}
}
next = now - step[now];
if (1 <= next)
{
if (dp[next] == -1)
{
dp[next] = dp[now] + 1;
q.push(next);
}
}
}
return -1;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
while (~scanf("%d", &n))
{
if (!n)
break;
scanf("%d%d", &fr, &to);
for (int i = 1; i <= n; i++)
scanf("%d", &step[i]);
printf("%d\n", bfs());
}
return 0;
}