当前位置:网站首页>深度优先搜索简要讲解(附带基础题)
深度优先搜索简要讲解(附带基础题)
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:十年磨一剑,霜刃未曾试
边栏推荐
- Detailed explanation of PPTC self recovery fuse
- No clue about the data analysis report? After reading this introduction of smartbi, you will understand!
- [untitled]
- 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
- @Scheduled scheduled tasks
- Which product is better for 2022 annual gold insurance?
- 1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
- (practice C language every day) pointer sorting problem
- Smart subway | cloud computing injects wisdom into urban subway transportation
- Why is it recommended that technologists write blogs?
猜你喜欢

Select sorting and bubble sorting template

mysql数据库的存储

If you have just joined a new company, don't be fired because of your mistakes

No clue about the data analysis report? After reading this introduction of smartbi, you will understand!

ctf-pikachu-CSRF

Package details_ Four access control characters_ Two details of protected

Development of digital collection trading platform development of digital collection platform

Mindmanager2022 efficient and easy to use office mind map MindManager
![[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush](/img/98/3e5f1094141e34d7e77f908e12acda.jpg)
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush

SQL语句加强练习(MySQL8.0为例)
随机推荐
Deep thinking on investment
[.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
How to pipe several commands in Go?
Wechat official account web page authorization
Reduce function under functools
Add token validation in swagger
pytest多进程/多线程执行测试用例
[database I] database overview, common commands, view the table structure of 'demo data', simple query, condition query, sorting data, data processing function (single row processing function), groupi
Leecode 122. Zuijia timing of buying and selling stocks ②
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
SQL statement strengthening exercise (MySQL 8.0 as an example)
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
Nbear introduction and use diagram
postgresql 用户不能自己创建表格配置
What are the virtual machine software? What are their respective functions?
潘多拉 IOT 开发板学习(HAL 库)—— 实验6 独立看门狗实验(学习笔记)
“软硬皆施”,助力建成新型云计算数据中心
Learning video website
Apple submitted the new MAC model to the regulatory database before the spring conference
Redis notes (I) Linux installation process of redis