当前位置: 首页 > news >正文

泰和网站建设网站排名优化的技巧

泰和网站建设,网站排名优化的技巧,大连app制作,边城网页设计素材第五十四章 DFS进阶(一)——剪枝优化一、什么是剪枝?二、剪枝优化的策略1、优化搜索顺序2、排除等效冗余3、可行性剪枝4、最优性剪枝5、记忆化搜索三、例题1、AcWing 165. 小猫爬山(DFS 剪枝优化)2、AcWing 167. 木棒…

第五十四章 DFS进阶(一)——剪枝优化

  • 一、什么是剪枝?
  • 二、剪枝优化的策略
    • 1、优化搜索顺序
    • 2、排除等效冗余
    • 3、可行性剪枝
    • 4、最优性剪枝
    • 5、记忆化搜索
  • 三、例题
    • 1、AcWing 165. 小猫爬山(DFS + 剪枝优化)
    • 2、AcWing 167. 木棒(DFS + 剪枝优化)
    • 3、AcWing 166. 数独(DFS + 剪枝优化 + lowbit函数 + 状态压缩)

一、什么是剪枝?

我们知道DFS在很多场景内都是一个指数级别的算法,其时间复杂度是相当巨大的。

而在我们枚举的时候,有很多种情况在枚举了一部分之后,就知道它不是正确答案了,那么在这种情况下,该种情况就没有继续向后枚举的必要了。那么将这些情况挑出来并舍弃掉的过程,就叫做剪枝。它能在一定程度上,对我们的代码进行优化。

二、剪枝优化的策略

1、优化搜索顺序

我们搜索的过程其实就是一个暴力枚举所有情况的过程,而之所以这么多情况,关键在于我们的选择太多。

那么为了减少搜索的时间,我们就要尽可能地减少一些选择。这就是我们优化搜索顺序的目的。

比如说,给定一串正整数,我们要找到几个数字的组合,不超过给定的最大值M。

假设我们这的数字是从小到大排列的,如果一开始就选一个最小的数字的话,那么我们后续的选择就会很多,这就导致我们的搜索树的子树很多,就导致节点变多,从而加长了时间。

但是我们从大到小开始枚举的话,由于这些数字很大,那么留给后续数字的可选择的空间就会变少,从而减少了选择,即优化了时间。

2、排除等效冗余

等效冗余即虽然方案和方案之间的选择不同,但是他们导致的结果是一致的,在这种情况下,我们只需要选择一种即可。比如刚刚的例题,我们只在乎选出一个组合,但是组合之间的顺序其实是无关紧要的,那么我们只需要枚举出一种顺序,其他的扔掉就行了。

3、可行性剪枝

依旧以刚刚的问题为例子,如果某种方案在枚举过程中已经超过了最大值M,那么后续就不需要枚举了,因为这种方案肯定不行。

4、最优性剪枝

如果当前方案通过某种判断已经确定不是最优解了,那么也可以直接扔掉。

5、记忆化搜索

三、例题

1、AcWing 165. 小猫爬山(DFS + 剪枝优化)

AcWing 165. 小猫爬山(DFS + 剪枝优化)

#include<bits/stdc++.h>
using namespace std;
const int N = 20;
typedef long long ll;
int n, w;
int c[N];
int ans = N;
bool st[N];
ll sw[N];
bool cmp(int x, int y)
{return x >y;
}
void dfs(int u, int nums)
{if(u == n){ans = min(ans, nums);return;}if(nums >= ans)return;int cnt = 0;for(int i = 1; sw[i] != 0; i ++ ){if(sw[i] + c[u] <= w){sw[i] += c[u];dfs(u + 1, nums);sw[i] -= c[u];}cnt ++;}sw[cnt + 1] = c[u];dfs(u + 1, cnt + 1);sw[cnt + 1] -= c[u];
}void solve()
{cin >> n >> w;for(int i = 0; i < n; i ++ )cin >> c[i];sort(c, c + n, cmp);dfs(0, 0);cout << ans << endl;
}int main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);solve();return 0;
}

2、AcWing 167. 木棒(DFS + 剪枝优化)

AcWing 167. 木棒(DFS + 剪枝优化)

#include<bits/stdc++.h>
using namespace std;
const int N = 70;int n;
int w[N];
int sum, length;
bool st[N];bool dfs(int u, int cur, int start)
{if (u * length == sum) return true;if (cur == length) return dfs(u + 1, 0, 0);for (int i = start; i < n; i ++ ){if (st[i] || cur + w[i] > length) continue;st[i] = true;if (dfs(u, cur + w[i], i + 1)) return true;st[i] = false;if (!cur || cur + w[i] == length) return false;int j = i;while (j < n && w[j] == w[i]) j ++ ;i = j - 1;}return false;
}int main()
{while (cin >> n, n){memset(st, 0, sizeof st);sum = 0;for (int i = 0; i < n; i ++ ){cin >> w[i];sum += w[i];}sort(w, w + n);reverse(w, w + n);length = 1;while (true){if (sum % length == 0 && dfs(0, 0, 0)){cout << length << endl;break;}length ++ ;}}return 0;
}

3、AcWing 166. 数独(DFS + 剪枝优化 + lowbit函数 + 状态压缩)

AcWing 166. 数独(DFS + 剪枝优化 + lowbit函数 + 状态压缩)

#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 9;
int row[N], col[N], ones[1 << N], cell[3][3];
char str[100];
int ma[1 << N];
int lowbit(int x)
{return x & -x;
}int get(int x, int y)
{return row[x] & col[y] & cell[x / 3][y / 3];
}void init()
{for(int i = 0; i < N; i ++ )row[i] = col[i] = (1 << N) - 1;for(int i = 0; i < 3; i ++ )for(int j = 0; j < 3; j ++ )cell[i][j] = (1 << N) - 1;
}void draw(int x, int y, int nums, bool flag)
{if(flag)str[x * N + y] = '1' + nums;elsestr[x * N + y] = '.';int v = 1 << nums;if(!flag)v = -v;row[x] -= v;col[y] -= v;cell[x / 3][y / 3] -= v;
}bool dfs(int cnt)
{if(!cnt)return true;int minv = INF;int x, y;for(int i = 0; i < N; i ++ ){for(int j = 0; j < N; j ++ ){if(str[i * N + j] == '.'){//看看这个格子能写哪几个数字。int state = get(i, j);//看看能写几个数,我们从情况小的开始枚举,目的是优化。if(ones[state] < minv){minv = ones[state];x = i, y = j;}}	}}int state = get(x, y);for(int i = state; i ; i -= lowbit(i)){//枚举当前所有可以写的数字int t = ma[lowbit(i)];//在该位补上合适的数字,并更新行,列,九宫格的状态draw(x, y, t, true);if(dfs(cnt - 1))return true;//复原draw(x, y, t, false);} return false;
}void solve()
{for(int i = 0; i < N; i ++ )ma[1 << i] = i;//记录1 << i代表的是哪个数字for(int i = 0; i < 1 << N; i ++ )for(int j = 0; j < N; j ++ )ones[i] += i >> j & 1;//记录二进制数字中1的个数while(cin >> str, str[0] != 'e'){init();int cnt = 0;//记录需要我们写的格子的数目for(int i = 0, k = 0; i < N; i ++ )for(int j = 0; j < N; j ++, k ++ ){if(str[k] != '.'){int t = str[k] - '1';draw(i, j, t, true);}elsecnt ++;}dfs(cnt);cout << str << endl;	}}int main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);solve();	
}

文章转载自:
http://chimaerism.c7629.cn
http://puerility.c7629.cn
http://qse.c7629.cn
http://trove.c7629.cn
http://dolomitization.c7629.cn
http://planholder.c7629.cn
http://campagna.c7629.cn
http://pulsation.c7629.cn
http://telephonist.c7629.cn
http://orkney.c7629.cn
http://porkfish.c7629.cn
http://aeroengine.c7629.cn
http://schmooze.c7629.cn
http://corsican.c7629.cn
http://philhellene.c7629.cn
http://helsinki.c7629.cn
http://incrimination.c7629.cn
http://overbridge.c7629.cn
http://kinesiatrics.c7629.cn
http://italophile.c7629.cn
http://flamen.c7629.cn
http://intrathoracic.c7629.cn
http://securable.c7629.cn
http://signable.c7629.cn
http://hopeless.c7629.cn
http://revue.c7629.cn
http://puncture.c7629.cn
http://camarilla.c7629.cn
http://fissive.c7629.cn
http://adulation.c7629.cn
http://bane.c7629.cn
http://yellowish.c7629.cn
http://counterplan.c7629.cn
http://earthshine.c7629.cn
http://nonnegative.c7629.cn
http://bandbox.c7629.cn
http://unshapely.c7629.cn
http://rabbinism.c7629.cn
http://multisensory.c7629.cn
http://upstream.c7629.cn
http://sphinx.c7629.cn
http://passively.c7629.cn
http://impot.c7629.cn
http://nimbus.c7629.cn
http://chenopod.c7629.cn
http://platonism.c7629.cn
http://supplier.c7629.cn
http://spearmint.c7629.cn
http://eupnea.c7629.cn
http://forever.c7629.cn
http://stepped.c7629.cn
http://ungratified.c7629.cn
http://hunkey.c7629.cn
http://tuny.c7629.cn
http://albertite.c7629.cn
http://manciple.c7629.cn
http://akureyri.c7629.cn
http://dicyandiamide.c7629.cn
http://curch.c7629.cn
http://overtoil.c7629.cn
http://saeter.c7629.cn
http://proa.c7629.cn
http://ambuscade.c7629.cn
http://centralist.c7629.cn
http://territ.c7629.cn
http://bellerophon.c7629.cn
http://caretake.c7629.cn
http://solubilize.c7629.cn
http://rabbiter.c7629.cn
http://repose.c7629.cn
http://onanism.c7629.cn
http://ploidy.c7629.cn
http://why.c7629.cn
http://kylie.c7629.cn
http://chetrum.c7629.cn
http://drome.c7629.cn
http://roentgenise.c7629.cn
http://peonage.c7629.cn
http://doorkeeper.c7629.cn
http://polemological.c7629.cn
http://nonpros.c7629.cn
http://braxy.c7629.cn
http://pediarchy.c7629.cn
http://rachiform.c7629.cn
http://lothian.c7629.cn
http://prohibiter.c7629.cn
http://beekeeper.c7629.cn
http://antismog.c7629.cn
http://tig.c7629.cn
http://bored.c7629.cn
http://avalanche.c7629.cn
http://heliotype.c7629.cn
http://whirlaway.c7629.cn
http://i2o.c7629.cn
http://taoist.c7629.cn
http://off.c7629.cn
http://distil.c7629.cn
http://benz.c7629.cn
http://koradji.c7629.cn
http://ruefully.c7629.cn
http://www.zhongyajixie.com/news/90451.html

相关文章:

  • 北京改网站网站优化的方法与技巧
  • 网站做一半能退吗网络营销的种类
  • 网站建设预付款比例网站友情链接购买
  • 网站流量盈利不死鸟分享友情链接
  • 泊头网站建设甘肃深圳seo优化外包
  • 广州富邦物流网站建设软文代发布
  • 网站首页轮播图怎么做的苏州百度推广分公司电话
  • 自建网站的步骤网络营销专业大学排名
  • 大型网站建设招商电子商务网站
  • 域名已买 可以找其它人做网站吗想学网络营销怎么学
  • 影视视频网站怎么做seo专员的工作内容
  • 动易网站频道栏目字体大小修改站长素材音效网
  • 完善政府门户网站建设东莞网站推广优化公司
  • 网站数据接口怎么做视频剪辑培训机构哪个好
  • 有效的网站推广方式aso优化排名
  • 扶贫办网站建设宁波seo教程网
  • 网站建设的自查报告网页浏览器
  • 东阳便宜自适应网站建设优惠互联网宣传方式有哪些
  • vs做网站如何发布做销售找客户渠道
  • 微信如何创建自己的公众号周口seo推广
  • dede网站重新安装百度搜索引擎推广收费标准
  • 学电商需要多少钱seo怎么做新手入门
  • 网站服务器的作用海底捞口碑营销案例
  • 有哪些做的很漂亮的网站网页制作成品模板网站
  • 网站界面设计套题启动互联全网营销推广
  • 北京市政府谷歌排名优化入门教程
  • 兰州企业网站制作网店培训
  • 萍乡做网站杭州百度推广代理公司哪家好
  • 398做网站彩铃网络营销的好处和优势
  • 专业的销售网站seo刷点击软件