实在不明白为什么大家会把这道题当dp
这明明是一道广搜题啊
队列里只需要记录三个变量
-
now 表示现在在第几个柱子
-
k 表示向下跳了几格
-
len 表示跳的次数
然后就是正常广搜的思路了(别忘记加hash)
以下是代码
#include<bits/stdc++.h>
#define LL long long// 有点害怕爆 int ,所以用了 long long
#define N 201
using namespace std;
struct ele
{
int now, k, len;// 结构体内的三个成员
};
queue <ele> q;
bool used[N][N];// hash
LL a[N], f[71];// f 预处理出了 2 的 i 次方
int n;
inline LL read()// 快速读入,可加可不加
{
LL f = 0, fu = 1;
char c = getchar();
while(c < '0' || c > '9')
{
if(c == '-') fu = -1;
c = getchar();
}
while(c >= '0' && c <= '9')
{
f = (f << 3) + (f << 1) + c - 48;
c = getchar();
}
return f * fu;
}
int main()
{
memset(used, 0, sizeof(used));
f[0] = 1;
for(int i = 1; i <= 66; i++) f[i] = f[i - 1] << 1;// 预处理
n = read();
for(int i = 1; i <= n; i++) a[i] = read();
q.push((ele){1, 0, 0});// 将起点加入队列
used[1][0] = 1;
while(!q.empty())// 普通广搜的思路啦
{
ele u = q.front(); q.pop();
if(u.now == n)// 如果到终点了,输出答案
{
cout << u.len;
return 0;
}
if(u.now != 1 && used[u.now - 1][u.k + 1] == 0)// 尝试退后一格
{
used[u.now - 1][u.k + 1] = 1;
q.push((ele){u.now - 1, u.k + 1, u.len + 1});
}
for(int i = u.now + 1; i <= n; i++)// 尝试向上跳跃
{
if(a[i] <= a[u.now] + f[u.k] && used[i][0] == 0)
{
used[i][0] = 1;
q.push((ele){i, 0, u.len + 1});
}
}
}
cout << -1;// 表示不能到
return 0;
}