当前位置:网站首页>[search] - iteration deepening / bidirectional dfs/ida*
[search] - iteration deepening / bidirectional dfs/ida*
2022-07-29 01:36:00 【Xuanche_】

Iteration deepening
Depth first search selects one branch at a time , Keep going deep , Do not backtrack until you reach the recursive boundary . There is a certain flaw in this strategy , Suppose that : Each node of the search tree has many branches , And the answer to the question is on a shallow node . If deep search chooses the wrong branch at the beginning , It is likely to waste a lot of time on the deep sub section tree that does not contain answers .
As shown in the figure below , The left half is the state space of the problem , The diamond represents the answer , Then the search tree generated by depth first search is shown in the following figure , The algorithm wastes a lot of time on the deep subtree circled by the matrix .

here , We can Limit the depth of search from small to large , If you can't find the answer at the current depth , Just increase the depth limit , Do a new search , This is it. Iteration deepening Thought .

Although the process is limited in depth to d When , Will repeat the search 1~d-1 The node of the layer , But when the number of search nodes and branches is large , As the number of layers goes deeper , Nodes of each layer will Exponential growth , This repeated search is compared with the scale of deep subtree , It's the little wizard who sees the big one .
To make a long story short , When the size of the search tree grows rapidly with the depth of the hierarchy , And we can ensure that the answer is at a shallow node , We can use depth first search algorithm to solve the problem .
AcWing 170. Addition sequence
sample input :
5 7 12 15 77 0sample output :
1 2 4 5 1 2 4 6 7 1 2 4 8 12 1 2 4 5 10 15 1 2 4 8 9 17 34 68 77
prune 1: Give priority to enumerating larger numbers
prune 2: Eliminate equivalent redundancy
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int n;
int path[N];
bool dfs(int u, int depth)
{
if(u > depth) return false;
if(path[u - 1] == n) return true;
bool st[N] = {0};
for(int i = u - 1; i >= 0; i -- )
for(int j = i; j >= 0; j --)
{
int s = path[i] + path[j];
if(s > n || s <= path[u - 1] || st[u]) continue;
st[s] = true;
path[u] = s;
if(dfs(u + 1, depth)) return true;
}
return false;
}
int main()
{
path[0] = 1;
while(cin >> n, n)
{
int depth = 1;
while(!dfs(1, depth)) depth ++ ;
for(int i = 0; i < depth ; i ++ ) cout << path[i] << ' ';
cout << endl;
}
return 0;
}AcWing 171. giving gifts ( two-way DFS)
sample input :
20 5 7 5 4 18 1sample output :
19
- Sort all items according to their weight from small to large
- Before K Make a list of all the weights that can be put together by an item , Then sort and weigh
- Search for the rest N-K How to choose an item , Then dichotomize no more than in the table W The maximum of
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 1 << 24;
int n, m, k;
int g[50], weights[N];
int cnt = 0;
int ans;
void dfs(int u, int s)
{
if (u == k)
{
weights[cnt ++ ] = s;
return;
}
if ((LL)s + g[u] <= m) dfs(u + 1, s + g[u]);
dfs(u + 1, s);
}
void dfs2(int u, int s)
{
if (u == n)
{
int l = 0, r = cnt - 1;
while (l < r)
{
int mid = l + r + 1 >> 1;
if (weights[mid] + (LL)s <= m) l = mid;
else r = mid - 1;
}
if (weights[l] + (LL)s <= m) ans = max(ans, weights[l] + s);
return;
}
if ((LL)s + g[u] <= m) dfs2(u + 1, s + g[u]);
dfs2(u + 1, s);
}
int main()
{
cin >> m >> n;
for (int i = 0; i < n; i ++ ) cin >> g[i];
sort(g, g + n);
reverse(g, g + n);
k = n / 2; // prevent n = 1 when , There's a dead cycle
dfs(0, 0);
sort(weights, weights + cnt);
int t = 1;
for (int i = 1; i < cnt; i ++ )
if (weights[i] != weights[i - 1])
weights[t ++ ] = weights[i];
cnt = t;
dfs2(k, 0);
cout << ans << endl;
return 0;
}
边栏推荐
- SiC功率半导体产业高峰论坛成功举办
- uniapp createSelectorQuery().select 获取返回null报错
- 一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
- Regular checksum time formatting
- 云原生应用综合练习下
- Django uses the existing data table method of MySQL database
- 瑞吉外卖项目实战Day01
- Interviewer: programmer, please tell me who leaked the company interview questions to you?
- Focus on differentiated product design, intelligent technology efficiency improvement and literacy education around new citizen Finance
- Analysis of Multi Chain use cases on moonbeam -- review of Derek's speech in Polkadot decoded 2022
猜你喜欢

Docuware mobile labor solution can help you build a new productivity model: anytime, anywhere, any device

Canal real-time parsing MySQL binlog data practice

Analysys analysis: focus on users, improve the user experience of mobile banking, and help the growth of user value

Redis is installed on Linux

一篇万字博文带你入坑爬虫这条不归路 【万字图文】

一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力

SiC Power Semiconductor Industry Summit Forum successfully held

Self-attention neural architecture search for semantic image segmentation

什么是原码、反码和补码

PlatoFarm社区生态福音,用户可借助Elephant Swap获得溢价收益
随机推荐
Understand various paths
J9数字论:什么因素决定NFT的价值?
redis安装,集群部署与常见调优
RHCE command practice (I)
J9 number theory: what factors determine the value of NFT?
Error reporting: SQL syntax error in flask. Fields in SQL statements need quotation marks when formatting
Flink SQL Hudi actual combat
【搜索】—— DFS之剪枝与优化
DVWA之SQL注入
New 1688 API access instructions
如何选择专业、安全、高性能的远程控制软件
els 新的方块落下
Rewriting method set
Redis is installed on Linux
围绕新市民金融聚焦差异化产品设计、智能技术提效及素养教育
Regular checksum time formatting
Subtotal of process thread coordination
Oozie job scheduling
[ManageEngine] help Harbin Engineering University realize integrated monitoring and management of network traffic
What are source code, inverse code and complement code

