当前位置:网站首页>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();
}
边栏推荐
- Leveldb source code analysis series - main process
- Persistence / caching of RDD in spark
- mysql根据两个字段去重
- Sql: stored procedures and triggers - Notes
- Intelligent online customer service system source code Gofly development log - 2 Develop command line applications
- Mysql相关术语
- Reinforcement learning - learning notes 5 | alphago
- Happy sound 2[sing.2]
- 1292_FreeROS中vTaskResume()以及xTaskResumeFromISR()的实现分析
- ViT论文详解
猜你喜欢
功能强大的国产Api管理工具
爬虫实战(五):爬豆瓣top250
PostgreSQL 修改数据库用户的密码
GPS从入门到放弃(十九)、精密星历(sp3格式)
基于LM317的可调直流电源
Enhance network security of kubernetes with cilium
GPS从入门到放弃(十二)、 多普勒定速
Make menuconfig has a recipe for target 'menuconfig' failed error
[Chongqing Guangdong education] Information Literacy of Sichuan Normal University: a new engine for efficiency improvement and lifelong learning reference materials
GPS du début à l'abandon (XIII), surveillance autonome de l'intégrité du récepteur (raim)
随机推荐
guava: Multiset的使用
Reptile practice (V): climbing watercress top250
Save and retrieve strings
GPS从入门到放弃(十九)、精密星历(sp3格式)
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
JPEG2000 matlab source code implementation
Record the process of cleaning up mining viruses
Sequoia China, just raised $9billion
1292_ Implementation analysis of vtask resume() and xtask resume fromisr() in freeros
Redistemplate common collection instructions opsforlist (III)
Some problems about the use of char[] array assignment through scanf..
PostgreSQL 安装gis插件 CREATE EXTENSION postgis_topology
guava:Collections.unmodifiableXXX创建的collection并不immutable
【sciter Bug篇】多行隐藏
Guava: three ways to create immutablexxx objects
Univariate cubic equation - relationship between root and coefficient
GPS从入门到放弃(十七) 、对流层延时
Guava: use of multiset
Z function (extended KMP)
PostgreSQL 修改数据库用户的密码