HDU 1548 (最基础的BFS了) A strange lift

时间:2023-03-09 22:14:07
HDU 1548 (最基础的BFS了) A strange lift

这是一维的BFS,而且没有什么变形,应该是最基础的BFS了吧

题意:

有这样一个奇葩的电梯,你在第i层的时候你只能选择上或者下Ki层,也就是你只能从第i层到达i+Ki或者i-Ki层。当然电梯最低只能在1层最高只能在n层。

给出起点和终点问最少需要多少次才能到达终点,如果不能到达输出-1

没有什么好解释的了,如此单纯的一道题,只要不是太粗心就能A过去

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = + ;
struct Point
{
int h;
int times;
}qu[maxn];
int n, from, to, go[maxn], vis[maxn];
int head, tail;
int dir[] = {, -}; void BFS(void)
{
head = , tail = ;
qu[].h = from, qu[].times = ;
while(head < tail)
{
if(qu[head].h == to)
{
printf("%d\n", qu[head].times);
return;
}
for(int i = ; i < ; ++i)
{
int hh = qu[head].h + go[qu[head].h]*dir[i];
if(hh> && hh<=n && (!vis[hh]))
{
vis[hh] = ;
qu[tail].h = hh;
qu[tail++].times = qu[head].times + ;
}
}
++head;
}
printf("-1\n");
} int main(void)
{
#ifdef LOCAL
freopen("1548in.txt", "r", stdin);
#endif while(scanf("%d", &n) == && n)
{
scanf("%d%d", &from, &to);
for(int i = ; i <= n; ++i)
scanf("%d", &go[i]);
memset(vis, , sizeof(vis));
BFS();
}
return ;
}

代码君