第六周
题解
1. [洛谷P1135] 奇怪的电梯
1-1. 题干
1-2. 解析
- 广搜,对于现有楼层上下k层,若满足楼层存在与未到达过两个条件,则向队列中push
1-3. 原码
#include <iostream>
#include <queue>
using namespace std;
struct step
{
int n;
int step;
};
queue<step> q;
int n, a, b, k[210];
bool vis[210];
int ans = -1;
int main()
{
cin >> n >> a >> b;
for (int i = 1; i <= n; i++)
{
cin >> k[i];
}
q.push({
a, 0});
while (!q.empty())
{
step now = q.front();
q.pop();
if (now.n == b)
{
ans = now.step;
break;
}
if (vis[now.n])
{
continue;
}
vis[now.n] = true;
int next1 = now.n + k[now.n];
int next2 = now.n - k[now.n];
if (next1 <= n && next1 >= 1)
{
q.push({
next1, now.step + 1});
}
if (next2 <= n && next2 >= 1)
{
q.push({
next2, now.step + 1});
}
}
cout << ans << endl;
return 0;
}
2. [洛谷P1443] 马的遍历
2-1. 题干
2-2. 解析
- 广搜,对于马走日的八个位置,若满足在棋盘内与未到达过两个条件,则向队列中push
2-3. 原码
#