当前位置:网站首页>Informatics Orsay all in one 1360: strange lift
Informatics Orsay all in one 1360: strange lift
2022-06-28 05:15:00 【Junyi_ noip】
【 Topic link 】
ybt 1360: Strange elevator (lift)
Luogu P1135 Strange elevator
【 Topic test site 】
1. Guang Shu
【 Their thinking 】
remember k[i]
For the first time i The number marked on the floor .
This problem is similar to the problem of finding the shortest path in a maze , This problem can be solved by extensive search .
Let the node contain the number of floors x And the number of keystrokes s, First let the starting floor a And the number of keystrokes 0 The team . One node per team u
, At this time, on the floor u.x
, The next accessible floor is u.x+k[u.x]
And u.x-k[u.x]
, As long as this floor exists , And I haven't visited , Then the number of floors of the new node is the next possible floor , The number of keystrokes of the new node is added to the number of keystrokes of the original node 1, Join the node in the team .
If the number of floors of the outgoing node is the target floor b, Then the number of keystrokes of this node is from a To b Minimum number of keystrokes .
【 Solution code 】
solution 1: Guang Shu
#include<bits/stdc++.h>
using namespace std;
#define N 205
struct Node
{
int x, s;//x: Floor number s: Number of buttons
Node(){
}
Node(int a, int b):x(a), s(b){
}
};
int n, a, b, np, k[N];
bool vis[N];//vis[i]: The first i The position has passed
int bfs()
{
queue<Node> que;
vis[a] = true;
que.push(Node(a, 0));// The initial state : In the a layer , Press 0 Time
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]};// Two possible next arrival floors
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;// If it doesn't arrive b layer
}
int main()
{
cin >> n >> a >> b;
for(int i = 1; i <= n; ++i)
cin >> k[i];
cout << bfs();
return 0;
}
边栏推荐
- 2022西式面点师(高级)考试试题模拟考试平台操作
- Store inventory management system source code
- 2022年材料员-通用基础(材料员)操作证考试题库及答案
- 通过例子学习Rust
- Interview: what are the similarities and differences between abstract classes and interfaces?
- [leetcode] 12. Integer to Roman numeral
- Performance degradation during dpdk source code testing
- !‘ Cat 'is not an internal or external command, nor is it a runnable program or batch file.
- 电源插座是如何传输电的?困扰小伙伴这么多年的简单问题
- 项目经理考完PMP就够了?不是的!
猜你喜欢
gorm事务体验
基于订单流工具,我们能看到什么?
羧酸研究:Lumiprobe 磺基花青7二羧酸
Learn Taiji Maker - mqtt Chapter 2 (IV) esp8266 reserved message application
Redis 的 最新windows 版本 5.0.14
吴恩达深度学习测验题:deeplearning.ai-week1-quiz
【LeetCode】12、整数转罗马数字
【牛客网刷题系列 之 Verilog快速入门】~ 四选一多路器
The short video local life section has become popular. How to grasp the new opportunities?
2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
随机推荐
基于订单流工具,我们能看到什么?
Distributed transaction - Final consistency scheme based on message compensation (local message table, message queue)
Amino dye research: lumiprobe fam amine, 6-isomer
2022西式面点师(高级)考试试题模拟考试平台操作
Unity out ref params
【SkyWalking】一口气学完分布式链路追踪SkyWalking
程序员-放羊娃
Cgo+gsoap+onvif learning summary: 8. Summary of arm platform cross compilation operation and common problems
乔布斯在斯坦福大学的演讲稿——Follow your heart
Unity out ref params
Differences between pragma and ifndef
Function reentry caused by Keil C51's data overlaying mechanism
Based on the order flow tool, what can we see?
电源插座是如何传输电的?困扰小伙伴这么多年的简单问题
Object detection with OpenCV
2022 high altitude installation, maintenance and removal examination questions and answers
刘海屏手机在部分页面通过[[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom得到底部安全区高度为0问题
C语言中函数是什么?编程中的函数与数学中的函数区别?理解编程语言中的函数
Learn Taiji Maker - mqtt Chapter 2 (IV) esp8266 reserved message application
Liuhaiping's mobile phone passes [[uiapplication sharedapplication] delegate] window. safeAreaInsets. The height of the bottom security zone is 0