当前位置:网站首页>深度优先搜索简要讲解(附带基础题)
深度优先搜索简要讲解(附带基础题)
2022-07-04 03:47:00 【CTGU-Yoghurt】
PS:要放假了,考试也快考完了,时间多了起来,有空便来写一篇博客,接下来博客更新会快一点,Thanks for your watching。
前言:该博客主要通过题目讲解给大家简要的介绍一下
题目链接:P1135 奇怪的电梯 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
题目:
题目思路:
简单的dfs
对原来的地方进行标记
并向下一个地方进行转移
每次更新步数最小值
注:
大家只要记住dfs的几个基础模板
很容易学会
dfs的注意点:
1.设定一个标记数组 vis[ ](注意标注的位置,使其不会影响到余下的情况)
2.dfs的设定一个要求的值(一般取能取的范围的最大)并在,满足其他的条件下不断的刷新它
3.弄清楚进行dfs操作后的状态、
代码详解:
#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)//如果到达该楼层,更新到达目标楼层的最少步数
{
minn = min(minn, sum);
}
vis[step] = 1;//对该位置进行标记,防止下次再走到该位置(优化)
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]);
}//存楼梯能往上下走的步数
if (a != b)//如果一开始不在要到的楼层
{
dfs(a, 0);
if (minn != 999999999)
cout << minn;
else
cout << -1;
}
else cout << 0;
return 0;
}
PS:十年磨一剑,霜刃未曾试
边栏推荐
- The property of judging odd or even numbers about XOR.
- Summary of Chinese remainder theorem
- Objective-C string class, array class
- Add IDM to Google browser
- 支持首次触发的 Go Ticker
- Aperçu du code source futur - série juc
- Which product is better for 2022 annual gold insurance?
- Zlmediakit compilation and webrtc push-pull flow testing
- Object oriented -- encapsulation, inheritance, polymorphism
- MySQL data query optimization -- data structure of index
猜你喜欢
MySQL is dirty
GUI Graphical user interface programming (XIV) optionmenu - what do you want your girlfriend to wear on Valentine's day
Rhcsa day 3
In my spare time, I like to write some technical blogs and read some useless books. If you want to read more of my original articles, you can follow my personal wechat official account up technology c
Go 语言入门很简单:Go 实现凯撒密码
Infiltration practice guest account mimikatz sunflower SQL rights lifting offline decryption
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
MySQL maxscale realizes read-write separation
Objective-C description method and type method
Detailed explanation of PPTC self recovery fuse
随机推荐
The difference between MCU serial communication and parallel communication and the understanding of UART
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
Objective-C string class, array class
Sword finger offer:55 - I. depth of binary tree
static hostname; transient hostname; pretty hostname
What are the virtual machine software? What are their respective functions?
Detailed explanation of PPTC self recovery fuse
Formulaire day05
[.NET + mqtt]. Mise en œuvre de la communication mqtt dans l'environnement net 6 et démonstration de code pour l'abonnement et la publication de messages bilatéraux du serveur et du client
Pytest multi process / multi thread execution test case
Leetcode51.n queen
system information
Katalon framework test web (XXVI) automatic email
logistic regression
Support the first triggered go ticker
No clue about the data analysis report? After reading this introduction of smartbi, you will understand!
super_ Subclass object memory structure_ Inheritance tree traceability
Stm32bug [stlink forced update prompt appears in keilmdk, but it cannot be updated]
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
Nbear introduction and use diagram