当前位置:网站首页>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
边栏推荐
- A review of reverse reinforcement learning at Virginia Tech (VT)
- Leetcode51.n queen
- Explain AI accelerator in detail: why is this the golden age of AI accelerator?
- Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
- 深入浅出对话系统——使用Transformer进行文本分类
- [paddleseg source code reading] paddleseg calculates Miou
- Smart subway | cloud computing injects wisdom into urban subway transportation
- JSON string conversion in unity
- [untitled]
- functools下的reduce函数
猜你喜欢
If you have just joined a new company, don't be fired because of your mistakes
Monitoring - Prometheus introduction
2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
Pytest multi process / multi thread execution test case
如何有效远程办公之我见 | 社区征文
Audio and video technology development weekly | 232
'2'&gt;' 10'==true? How does JS perform implicit type conversion?
1289_FreeRTOS中vTaskSuspend()接口实现分析
Penetration practice - sqlserver empowerment
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
随机推荐
Katalon使用script实现查询List大小
What kind of experience is it when the Institute earns 20000 yuan a month!
Database SQL statement summary, continuous update
Smart subway | cloud computing injects wisdom into urban subway transportation
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
Defensive programming skills
MySQL backup notes
Mitsubishi M70 macro variable reading Mitsubishi M80 public variable acquisition Mitsubishi CNC variable reading acquisition Mitsubishi CNC remote tool compensation Mitsubishi machine tool online tool
MySQL one master multiple slaves + linear replication
Session learning diary 1
Value transfer communication between components (parent to child, child to parent, brother component to value)
warning: LF will be replaced by CRLF in XXXXXX
Class summation, shortest row
还原窗口位置的微妙之处
pytest多进程/多线程执行测试用例
Which product is better if you want to go abroad to insure Xinguan?
Detailed explanation of PPTC self recovery fuse
【读书会第十三期】多媒体处理工具 FFmpeg 工具集
[Yugong series] go teaching course 002 go language environment installation in July 2022
如何有效远程办公之我见 | 社区征文