当前位置:网站首页>信息学奥赛一本通 1360:奇怪的电梯(lift)
信息学奥赛一本通 1360:奇怪的电梯(lift)
2022-06-28 04:36:00 【君义_noip】
【题目链接】
ybt 1360:奇怪的电梯(lift)
洛谷 P1135 奇怪的电梯
【题目考点】
1. 广搜
【解题思路】
记k[i]为第i层楼上标记的数字。
该题与求迷宫中最短路径问题相似,可以用广搜方法来解决该问题。
设结点包含楼层数x与按键次数s,首先让起始楼层a与按键次数0入队。每出队一个结点u,此时在楼层u.x,下一次可以到达的楼层有u.x+k[u.x]与u.x-k[u.x],只要这个楼层是存在的,且没有访问过,那么新的结点的楼层数就是下一次可能到达的楼层,新的结点的按键次数在原来按键次数基础上加1,将该结点入队。
如果出队的结点的楼层数是目标楼层b,那么该结点的按键次数就是从a到b最少的按键次数。
【题解代码】
解法1:广搜
#include<bits/stdc++.h>
using namespace std;
#define N 205
struct Node
{
int x, s;//x:楼层数 s:按键次数
Node(){
}
Node(int a, int b):x(a), s(b){
}
};
int n, a, b, np, k[N];
bool vis[N];//vis[i]:第i位置已经走过
int bfs()
{
queue<Node> que;
vis[a] = true;
que.push(Node(a, 0));//初始状态:在第a层,按0次
while(que.empty() == false)
{
Node u = que.front();
que.pop();
if(u.x == b)
return u.s;
int x[2] = {
u.x + k[u.x], u.x - k[u.x]};//两个可能的下一次到达的楼层
for(int i = 0; i < 2; ++i)
{
if(x[i] >= 1 && x[i] <= n && vis[x[i]] == false)
{
vis[x[i]] = true;
que.push(Node(x[i], u.s + 1));
}
}
}
return -1;//如果没有到达b层
}
int main()
{
cin >> n >> a >> b;
for(int i = 1; i <= n; ++i)
cin >> k[i];
cout << bfs();
return 0;
}
边栏推荐
猜你喜欢

LeetCode 88:合并两个有序数组

mysql----where 1=1是什么意思

别卷!如何高质量地复现一篇论文?

Ppt production tips

UI automation test framework construction - write an app automation

Audio and video technology development weekly

Feign通过自定义注解实现路径的转义

2022年中國音頻市場年度綜合分析

With favorable policies, more than 20 provinces and cities have launched the yuanuniverse development plan

Play with double pointer
随机推荐
Mask's miserable and inspirational childhood, who is introverted by campus violence
[NOIP2002 普及组] 过河卒
判断对象中是否存在某一个属性
How to clean the nozzle of Epson l3153 printer
Congratulations to myself, official account has more than ten thousand fans
xml&nbsp; File read / write
Live online source code, JS dynamic effect, sidebar scrolling fixed effect
创新之源 理解通透 二
Performance optimization and implementation of video codec
[applet] solution document using font awesome Font Icon (picture and text)
Difference between curdate() and now()
Ppt production tips
Matlab exercises -- routine operation of matrix
多线程实现 重写run(),怎么注入使用mapper文件操作数据库
2022高处安装、维护、拆除考试题及答案
Bitlock recovery occurs in win 10, and the blue screen error code is 0x1600007e
UI自动化测试框架搭建 —— 编写一个APP自动化
PHP code wechat, official account and enterprise wechat send emoticons [u+1f449]
汇编常用指令
代码理解:IMPROVING CONVOLUTIONAL MODELS FOR HANDWRITTEN TEXT RECOGNITION