当前位置:网站首页>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
边栏推荐
猜你喜欢

Development of digital collection trading platform development of digital collection platform

Exercices de renforcement des déclarations SQL (MySQL 8.0 par exemple)

深度优先搜索简要讲解(附带基础题)

Katalon中控件的参数化

ctf-pikachu-CSRF

ctf-pikachu-CSRF

三年进账35.31亿,这个江西老表要IPO了

Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..

拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
![[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush](/img/94/2bdc31ec05595dbbc8a7a8d6b22252.jpg)
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
随机推荐
Sales management system of lightweight enterprises based on PHP
My opinion on how to effectively telecommute | community essay solicitation
MySQL one master multiple slaves + linear replication
Pytest multi process / multi thread execution test case
Calculate the odd sum of 1~n (1~100 as an example)
2022-07-03: there are 0 and 1 in the array. Be sure to flip an interval. Flip: 0 becomes 1, 1 becomes 0. What is the maximum number of 1 after turning? From little red book. 3.13 written examination.
Katalon框架测试web(二十一)获取元素属性断言
Es network layer
MySQL backup notes
SQL语句加强练习(MySQL8.0为例)
Objective-C string class, array class
Select sorting and bubble sorting template
投资深度思考
Development of digital collection trading platform development of digital collection platform
postgresql 用户不能自己创建表格配置
Day05 錶格
疫情来袭--远程办公之思考|社区征文
Wechat official account web page authorization
STM32外接DHT11显示温湿度
Eh, the log time of MySQL server is less than 8h?