当前位置:网站首页>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
边栏推荐
- logistic regression
- CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
- vim映射命令
- Objective-C string class, array class
- Infiltration practice guest account mimikatz sunflower SQL rights lifting offline decryption
- What kind of experience is it when the Institute earns 20000 yuan a month!
- XSS prevention
- Epidemic strikes -- Thinking about telecommuting | community essay solicitation
- ctf-pikachu-XSS
- Es network layer
猜你喜欢

logistic regression

Detailed explanation of PPTC self recovery fuse

函数计算异步任务能力介绍 - 任务触发去重

2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。

图解网络:什么是热备份路由器协议HSRP?

Tcpclientdemo for TCP protocol interaction

Recursive structure

Session learning diary 1

Go 语言入门很简单:Go 实现凯撒密码

基于PHP的轻量企业销售管理系统
随机推荐
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
STM32 external DHT11 display temperature and humidity
Summary of Chinese remainder theorem
【读书会第十三期】视频文件的封装格式
深入浅出对话系统——使用Transformer进行文本分类
Deep thinking on investment
vim映射命令
Consul of distributed service registration discovery and unified configuration management
What kind of experience is it when the Institute earns 20000 yuan a month!
投资深度思考
1289_FreeRTOS中vTaskSuspend()接口实现分析
JDBC 进阶
[paddleseg source code reading] normalize operation of paddleseg transform
Objective-C member variable permissions
Defensive programming skills
vim正确加区间注释
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?
Is it really so difficult to learn redis? Today, a fan will share his personal learning materials!
mysql数据库的存储
图解网络:什么是热备份路由器协议HSRP?