当前位置:网站首页>Brief explanation of depth first search (with basic questions)
Brief explanation of depth first search (with basic questions)
2022-07-04 04:01:00 【CTGU-Yoghurt】
PS: It's time for a holiday , The exam is almost over , More time , Come and write a blog when you are free , Next, blog updates will be faster ,Thanks for your watching.
Preface : This blog mainly gives you a brief introduction through topic explanation
Topic link :P1135 Strange elevator - Luogu | New ecology of computer science education (luogu.com.cn)
subject :
Topic ideas :
ordinary dfs
Mark the original place
And move to the next place
Minimum number of steps per update
notes :
Just remember dfs Several basic templates
It's easy to learn
dfs Points for attention :
1. Set an array of tags vis[ ]( Pay attention to the marked position , So that it will not affect the rest of the situation )
2.dfs Set a required value ( Generally, the maximum range that can be taken ) And in , Meet other conditions and constantly refresh it
3. Make sure to proceed dfs Status after operation 、
Code details :
#include<stdio.h>
#include<iostream>
using namespace std;
long long arr[206];
int vis[206];
int minn = 999999999;
int n, a, b;
void dfs(int step,int sum)
{
if (step == b)// If you arrive at this floor , Update the minimum number of steps to reach the target floor
{
minn = min(minn, sum);
}
vis[step] = 1;// Mark this position , Avoid going to this position next time ( Optimize )
if (step + arr[step] <= n && sum < minn&&vis[step + arr[step]] != 1) {
dfs(step + arr[step], sum + 1);
}
if (step - arr[step] >= 1 && sum < minn && vis[step - arr[step]] != 1) {
dfs(step - arr[step], sum + 1);
}
vis[step] = 0;
}
int main()
{
cin >> n>>a>>b;
for (int i = 1; i <= n; i++)
{
scanf("%lld", &arr[i]);
}// Save the steps that the stairs can go up and down
if (a != b)// If you are not on the floor to be reached at the beginning
{
dfs(a, 0);
if (minn != 999999999)
cout << minn;
else
cout << -1;
}
else cout << 0;
return 0;
}
PS: Ten years to sharpen a sword , Frost blade never tried
边栏推荐
- mysql数据库的存储
- AAAI2022 | Word Embeddings via Causal Inference: Gender Bias Reducing and Semantic Information Preserving
- Es network layer
- 如何有效远程办公之我见 | 社区征文
- Katalon框架测试web(二十六)自动发邮件
- [book club issue 13] packaging format of video files
- "Implement both software and hardware" to help build a new cloud computing data center
- Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
- EV6 helps the product matrix, and Kia is making efforts in the high-end market. The global sales target in 2022 is 3.15 million?
- 2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
猜你喜欢
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
Why is it recommended that technologists write blogs?
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
Typical applications of minimum spanning tree
mysql数据库的存储
'2'&gt;' 10'==true? How does JS perform implicit type conversion?
Consul of distributed service registration discovery and unified configuration management
What kind of experience is it when the Institute earns 20000 yuan a month!
Reduce function under functools
深度优先搜索简要讲解(附带基础题)
随机推荐
Third party login initial version
数据库SQL语句汇总,持续更新......
How about the ratings of 2022 Spring Festival Gala in all provinces? Map analysis helps you show clearly!
SQL語句加强練習(MySQL8.0為例)
How to pipe several commands in Go?
Value transfer communication between components (parent to child, child to parent, brother component to value)
National standard gb28181 protocol platform easygbs fails to start after replacing MySQL database. How to deal with it?
MySQL is dirty
Apple submitted the new MAC model to the regulatory database before the spring conference
Audio and video technology development weekly | 232
How much does it cost to open a futures account in China? Where is it safe to open an account at present?
ctf-pikachu-CSRF
微信公众号网页授权
Object oriented -- encapsulation, inheritance, polymorphism
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
潘多拉 IOT 开发板学习(HAL 库)—— 实验6 独立看门狗实验(学习笔记)
vue多级路由嵌套怎么动态缓存组件
Session learning diary 1
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
CSP drawing