当前位置:网站首页>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
边栏推荐
- Value transfer communication between components (parent to child, child to parent, brother component to value)
- Summary of Chinese remainder theorem
- 选择排序与冒泡排序模板
- functools下的reduce函数
- Third party login initial version
- Go 语言入门很简单:Go 实现凯撒密码
- Deep thinking on investment
- PostgreSQL users cannot create table configurations by themselves
- Objective-C description method and type method
- Defensive programming skills
猜你喜欢
Getting started with the go language is simple: go implements the Caesar password
Recursive structure
Don't disagree, this is the most powerful "language" of the Internet
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
logistic regression
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
基于PHP的轻量企业销售管理系统
mysql数据库的存储
[untitled]
Detailed explanation of PPTC self recovery fuse
随机推荐
Learning video website
1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
Calculate the odd sum of 1~n (1~100 as an example)
STM32外接DHT11显示温湿度
vim正确加区间注释
Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
STM32 external DHT11 display temperature and humidity
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.
How to dynamically cache components in Vue multi-level route nesting
Introduction to asynchronous task capability of function calculation - task trigger de duplication
Monitoring - Prometheus introduction
ctf-pikachu-XSS
How to pipe several commands in Go?
智慧地铁| 云计算为城市地铁交通注入智慧
Nbear introduction and use diagram
Typical applications of minimum spanning tree
warning: LF will be replaced by CRLF in XXXXXX
Objective-C description method and type method
Value transfer communication between components (parent to child, child to parent, brother component to value)
What kind of experience is it when the Institute earns 20000 yuan a month!