当前位置:网站首页>Search element topic (DFS)
Search element topic (DFS)
2022-07-06 21:59:00 【Joanh_ Lan】
Search element topic (DFS )
Preface
Searching is a violent method
It can be understood according to the tree
Without cutting the branch , Can put all * programme * “ enumeration ” come out
Scissor branch --> When it is certain that it will not be the solution , Early termination of the " spot " The following search elements
Similar to tree structure
A problem can be divided into several sub parts to solve ( somewhat DP It smells good. , But this lesson is just a simple search , Direction consumption )
In other words :
If the problem is abstracted into a set , Each element can be regarded as a local scheme
all A subset of elements May be a solution to the problem
If you feel a little dizzy
Let's have a practical question to help the fun
123 These three numbers are all arranged
Every set on the graph , Is a solution to the problem
Such as :3 1 2
Full Permutation implementation code
#include <bits/stdc++.h>
#define buff \ ios::sync_with_stdio(false); \ cin.tie(0);
//#define int long long
using namespace std;
const int N = 1000;
bool st[N];
int ans[N];
int n;
void dfs(int cnt)
{
if (cnt == n)
{
for (int i = 1; i <= n; i++)
{
cout << ans[i];
cout << (i == n ? '\n' : ' ');
}
}
for (int i = 1; i <= n; i++)
{
if (!st[i])
{
ans[cnt + 1] = i;
st[i] = 1;
dfs(cnt + 1);
st[i] = 0;
}
}
}
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
ans[1] = i;
st[i] = 1;
dfs(1);
st[i] = 0;
}
}
int main()
{
solve();
}
Templates
void dfs( Parameters to be used )
{
if( Meet the conditions of the topic )
{
xxx
}
if( The boundary conditions | Scissor branch )
{
xxx
return
}
agile dfs The process
Write according to the meaning of the title , Very flexible !
How can I feel comfortable (:
for ( Conditions )
{
xxx
xxx
st[idx] = 1;//****
dfs(xxx)
st[idx] = 0//****
}
or
dfs(xxx)
wait
cout << ans << '\n';
}
subject
P2392 kkksc03 Cramming before the exam
https://www.luogu.com.cn/problem/P2392
AC Code
#include <bits/stdc++.h>
#define buff \ ios::sync_with_stdio(false); \ cin.tie(0);
//#define int long long
using namespace std;
const int N = 1000;
int a[N];
int n1, n2, n3, n4;
int ans = 0, tt = 0;
int nn;
void dfs(int l, int r, int cnt)
{
if (cnt == nn)
{
tt = min(tt,max(l, r));
return;
}
dfs(l + a[cnt + 1], r, cnt + 1);
dfs(l, r + a[cnt + 1], cnt + 1);
}
void solve()
{
cin >> n1 >> n2 >> n3 >> n4;
for (int i = 1; i <= n1; i++)
cin >> a[i];
nn = n1, tt = 0x3f3f3f3f;
dfs(0, 0, 0);
ans += tt;
for (int i = 1; i <= n2; i++)
cin >> a[i];
nn = n2, tt = 0x3f3f3f3f;
dfs(0, 0, 0);
ans += tt;
for (int i = 1; i <= n3; i++)
cin >> a[i];
nn = n3, tt = 0x3f3f3f3f;
dfs(0, 0, 0);
ans += tt;
for (int i = 1; i <= n4; i++)
cin >> a[i];
nn = n4, tt = 0x3f3f3f3f;
dfs(0, 0, 0);
ans += tt;
cout << ans << endl;
}
int main()
{
solve();
}
P1135 Strange elevator
https://www.luogu.com.cn/problem/P1135
T Code
Too violent , Failed to deal with repeated local schemes
In other words : Cutting and supporting treatment is not in place
#include <bits/stdc++.h>
#define buff \ ios::sync_with_stdio(false); \ cin.tie(0);
//#define int long long
using namespace std;
const int N = 1000;
int s[N];
int n, bg, ed;
int ans = 0x3f3f3f3f;
void dfs(int idx, int cnt)
{
if (idx <= 0 || cnt > n)
return ;
if (idx == ed)
{
ans = min(ans, cnt);
return ;
}
dfs(idx + s[idx],cnt + 1);
dfs(idx - s[idx],cnt + 1);
}
void solve()
{
cin >> n >> bg >> ed;
for (int i = 1; i <= n; i++)
cin >> s[i];
dfs(bg,0);
cout << (ans == 0x3f3f3f3f ? -1 : ans) << '\n';
}
int main()
{
solve();
}
AC Code
#include <bits/stdc++.h>
#define buff \ ios::sync_with_stdio(false); \ cin.tie(0);
//#define int long long
using namespace std;
const int N = 1000;
int s[N];
int n, bg, ed;
int ans = 0x3f3f3f3f;
bool st[N];
void dfs(int idx, int cnt)
{
if (idx <= 0 || cnt > n || st[idx] || idx > n || cnt >= ans)
return ;
if (idx == ed)
{
ans = min(ans, cnt);
return ;
}
st[idx] = 1;
dfs(idx + s[idx],cnt + 1);
dfs(idx - s[idx],cnt + 1);
st[idx] = 0;
}
void solve()
{
cin >> n >> bg >> ed;
for (int i = 1; i <= n; i++)
cin >> s[i];
dfs(bg,0);
cout << (ans == 0x3f3f3f3f ? -1 : ans) << '\n';
}
int main()
{
solve();
}
This topic の summary
1. Correct use of marks
2. Judgment of shear branch & Optimize
...
Count class
P1605 maze
https://www.luogu.com.cn/problem/P1605
AC Code
#include <bits/stdc++.h>
#define buff \ ios::sync_with_stdio(false); \ cin.tie(0);
//#define int long long
using namespace std;
const int N = 1000;
int n, m, t;
int bgx, bgy, edx, edy;
int s[N][N];
bool st[N][N];
int xx[4] = {
0, 1, -1, 0}, yy[4] = {
1, 0, 0, -1};
int ans;
void dfs(int x, int y)
{
if (x == edx && y == edy)
{
ans++;
return;
}
for (int i = 0; i < 4; i++)
{
int tx = x + xx[i], ty = y + yy[i];
if ((tx >= 1 && tx <= n) && (ty >= 1 && ty <= m) && !s[tx][ty] && !st[tx][ty])
{
st[x][y] = 1;
dfs(tx, ty);
st[x][y] = 0;
}
}
}
void solve()
{
cin >> n >> m >> t;
cin >> bgx >> bgy >> edx >> edy;
for (int i = 1; i <= t; i++)
{
int a, b;
cin >> a >> b;
s[a][b] = 1;
}
dfs(bgx, bgy);
cout << ans << endl;
}
int main()
{
solve();
}
边栏推荐
- Sql: stored procedures and triggers - Notes
- ViT论文详解
- Microsoft technology empowerment position - February course Preview
- Why is the cluster mode of spark on Yan better than the client mode
- NPM run dev start project error document is not defined
- Persistence / caching of RDD in spark
- string的底层实现
- 嵌入式常用计算神器EXCEL,欢迎各位推荐技巧,以保持文档持续更新,为其他人提供便利
- Yyds dry goods inventory C language recursive implementation of Hanoi Tower
- 功能强大的国产Api管理工具
猜你喜欢
Shell product written examination related
AI 企业多云存储架构实践 | 深势科技分享
Make menuconfig has a recipe for target 'menuconfig' failed error
数字化转型挂帅复产复工,线上线下全融合重建商业逻辑
关于程序员的职业操守,从《匠艺整洁之道》谈起
numpy 下载安装
PostgreSQL 修改数据库用户的密码
GPS from getting started to giving up (16), satellite clock error and satellite ephemeris error
爬虫实战(五):爬豆瓣top250
Yuan Xiaolin: safety is not only a standard, but also Volvo's unchanging belief and pursuit
随机推荐
guava:创建immutableXxx对象的3种方式
GPS from getting started to giving up (XIII), receiver autonomous integrity monitoring (RAIM)
ViT论文详解
LeetCode:1189. The maximum number of "balloons" -- simple
Vit paper details
GPS from entry to abandonment (XVII), tropospheric delay
强化学习-学习笔记5 | AlphaGo
numpy 下载安装
JPEG2000-Matlab源码实现
Redistemplate common collection instructions opsforset (V)
MySQL - transaction details
设置状态栏样式Demo
GPS从入门到放弃(二十)、天线偏移
搜素专题(DFS )
jvm:大对象在老年代的分配
LeetCode学习记录(从新手村出发之杀不出新手村)----1
1292_ Implementation analysis of vtask resume() and xtask resume fromisr() in freeros
关于char[]数组通过scanf赋值使用上的一些问题。。
string的底层实现
GPS du début à l'abandon (XIII), surveillance autonome de l'intégrité du récepteur (raim)