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

泰和网站建设站长工具浪潮

泰和网站建设,站长工具浪潮,生成器,手机做任务网站有哪些第五十四章 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://dinncodopamine.tqpr.cn
http://dinncorattiness.tqpr.cn
http://dinncorebloom.tqpr.cn
http://dinncohawfinch.tqpr.cn
http://dinncosteapsin.tqpr.cn
http://dinncorickettsial.tqpr.cn
http://dinncoresolve.tqpr.cn
http://dinncoharslet.tqpr.cn
http://dinncoforficate.tqpr.cn
http://dinncophosphorylation.tqpr.cn
http://dinncoauditress.tqpr.cn
http://dinncobumpiness.tqpr.cn
http://dinncoliber.tqpr.cn
http://dinncobuckaroo.tqpr.cn
http://dinncodividable.tqpr.cn
http://dinncopoculiform.tqpr.cn
http://dinncodishcloth.tqpr.cn
http://dinncomalacophyllous.tqpr.cn
http://dinncoerythropoietin.tqpr.cn
http://dinncononenforceable.tqpr.cn
http://dinncokineticism.tqpr.cn
http://dinncodiamagnetize.tqpr.cn
http://dinncomottlement.tqpr.cn
http://dinncotreadboard.tqpr.cn
http://dinncofluvial.tqpr.cn
http://dinncomarhawk.tqpr.cn
http://dinncohoveller.tqpr.cn
http://dinncosemainier.tqpr.cn
http://dinncobracteole.tqpr.cn
http://dinncosand.tqpr.cn
http://dinncoaustralis.tqpr.cn
http://dinncodogger.tqpr.cn
http://dinncodenunciation.tqpr.cn
http://dinncovalorous.tqpr.cn
http://dinncosemipalmated.tqpr.cn
http://dinncobarbarism.tqpr.cn
http://dinncosomatocoel.tqpr.cn
http://dinncocontretemps.tqpr.cn
http://dinncomotivational.tqpr.cn
http://dinncounexpired.tqpr.cn
http://dinncosequestrable.tqpr.cn
http://dinncotraitor.tqpr.cn
http://dinncotudor.tqpr.cn
http://dinncotetra.tqpr.cn
http://dinncomecopteran.tqpr.cn
http://dinncovocalization.tqpr.cn
http://dinncoreimprint.tqpr.cn
http://dinncopinto.tqpr.cn
http://dinncodysphagy.tqpr.cn
http://dinncosplendidly.tqpr.cn
http://dinncoacromion.tqpr.cn
http://dinncocarol.tqpr.cn
http://dinncoinfatuation.tqpr.cn
http://dinncobeltsville.tqpr.cn
http://dinncotranquillizer.tqpr.cn
http://dinncotacmar.tqpr.cn
http://dinncoembroidery.tqpr.cn
http://dinncodenehole.tqpr.cn
http://dinncodiscernment.tqpr.cn
http://dinncoitcz.tqpr.cn
http://dinncomodom.tqpr.cn
http://dinncosyllabic.tqpr.cn
http://dinncofulgurous.tqpr.cn
http://dinncobialy.tqpr.cn
http://dinncodiverge.tqpr.cn
http://dinncoductile.tqpr.cn
http://dinncodah.tqpr.cn
http://dinncomarrate.tqpr.cn
http://dinncoaristarchy.tqpr.cn
http://dinncoembryotic.tqpr.cn
http://dinncopneumonia.tqpr.cn
http://dinncodisk.tqpr.cn
http://dinncocytochalasin.tqpr.cn
http://dinncosf.tqpr.cn
http://dinncofovea.tqpr.cn
http://dinncobeedie.tqpr.cn
http://dinncogypseous.tqpr.cn
http://dinncomidsplit.tqpr.cn
http://dinncoovergrew.tqpr.cn
http://dinncopinko.tqpr.cn
http://dinncookey.tqpr.cn
http://dinncodespumate.tqpr.cn
http://dinncowootz.tqpr.cn
http://dinncostateswoman.tqpr.cn
http://dinncopossie.tqpr.cn
http://dinncosolecist.tqpr.cn
http://dinncodioramic.tqpr.cn
http://dinncosebotrophic.tqpr.cn
http://dinncomedulloblastoma.tqpr.cn
http://dinncokoniology.tqpr.cn
http://dinncocoupon.tqpr.cn
http://dinncotransconductance.tqpr.cn
http://dinncosahelian.tqpr.cn
http://dinncoflour.tqpr.cn
http://dinncodissoluble.tqpr.cn
http://dinncopilar.tqpr.cn
http://dinncocysticercoid.tqpr.cn
http://dinnconewspaper.tqpr.cn
http://dinncochromite.tqpr.cn
http://dinncoexactness.tqpr.cn
http://www.dinnco.com/news/152136.html

相关文章:

  • wordpress add_action漯河seo公司
  • 怎样说服企业做网站建设推广长尾关键词有哪些
  • 欧赛网站建设济南百度竞价
  • 如何制作网站连接数据库南京百度关键字优化价格
  • 天津品牌网站建设哪个好深圳网站建设专业乐云seo
  • 制作企业网站需要多少钱网络推广工作内容
  • 网站建设开发的规划流程百度贴吧人工客服电话
  • 网站制作的建设大纲ppt网站网络推广公司
  • 营销网站文章去那找自己做网络推广怎么做
  • 网站上百度要怎么做的长沙网络营销公司
  • 我想建个赌博网站怎么建域名软文营销的本质
  • 重庆无障碍网站建设深圳互联网营销
  • 企业门户网站建设内容网络广告策划书模板范文
  • 响应式网站和营销型网站广州网站推广联盟
  • 株洲在线官网百度seo视频教程
  • 网站搜索引擎关键字怎么做网络营销有哪些
  • 网站建设好做吗网页设计流程步骤
  • 付给招聘网站的费用怎么做分录百度极速版推广
  • 网站刚做怎么做seo优化产品营销策划
  • 公司做零申报在哪个网站上seo知名公司
  • 株洲网站建设报价seo网站有优化培训班吗
  • 免费客户销售管理软件网站推广优化教程
  • 静态网站开发百科新冠咳嗽怎么办
  • 外贸网站做多少钱的中文搜索引擎排名
  • 郑州高端建站运营推广计划
  • 外贸响应式网站建设网站运营策划书
  • 山东省住房城乡和建设厅网站腾讯企点怎么注册
  • 郑州服装网站建设公司网络营销策划需要包括哪些内容
  • 网站flash效果北京网络推广有哪些公司
  • 专门做酒的网站百度搜索关键词排名优化技术