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

四川网站建设套餐windows优化软件哪个好

四川网站建设套餐,windows优化软件哪个好,中国空间站拒绝日本,天津 网站建设文章目录题单一、模板 [极为重要]全排列DFS组合型DFS指数DFS二、专题烤鸡 (指数BFS)P1088 火星人 【全排列】P1149 火彩棒 [预处理 ]P2036 PERKETP1135 奇怪的电梯 暴力P1036 [NOIP2002 普及组] 选数 (组合)P1596 [USACO10OCT]Lake Counting …

文章目录

    • 题单
  • 一、模板 [极为重要]
    • 全排列DFS
    • 组合型DFS
    • 指数DFS
  • 二、专题
    • 烤鸡 (指数BFS)
    • P1088 火星人 【全排列】
    • P1149 火彩棒 [预处理 ]
    • P2036 PERKET
    • P1135 奇怪的电梯 暴力
    • P1036 [NOIP2002 普及组] 选数 (组合)
    • P1596 [USACO10OCT]Lake Counting S 【DFS求图的连通块】
    • 八数码

【DFS专题】优质题单推荐 10道题(C++ | 洛谷 | acwing)

题单

  • 来自b站大佬的题单
    image-20230324172048703

一、模板 [极为重要]

全排列DFS

在这里插入图片描述

  • 每个位置选什么数
#include<iostream>
using namespace std;
const int N = 10;
int path[N];//保存序列
int state[N];//数字是否被用过
int n;
void dfs(int u)
{if(u > n)//数字填完了,输出{for(int i = 1; i <= n; i++)//输出方案cout << path[i] << " ";cout << endl;}for(int i = 1; i <= n; i++)//空位上可以选择的数字为:1 ~ n{if(!state[i])//如果数字 i 没有被用过{path[u] = i;//放入空位state[i] = 1;//数字被用,修改状态dfs(u + 1);//填下一个位state[i] = 0;//回溯,取出 i}}
}int main() {cin >> n;dfs(1);
}

组合型DFS

在这里插入图片描述

  • 与全排列的差别就是第二个for循环开始的位置,换句话说就是每个数该放什么位置。
#include <bits/stdc++.h>
using namespace std;
const int N = 30;
int path[N];
int n, m;void dfs (int u, int start ) {//u:层数  start:起始的数值if (u > m) {for (int i = 1; i <= m; i ++ ) {cout << path[i] << ' ';}puts("");}else {for (int i = start; i <= n; i ++) {//path[u] = i;//表示已经填了dfs(u + 1, i + 1);//递归下一层path[u] = 0;//恢复现场}}
} int main () {cin >> n >> m;dfs(1,1); //第一层开始  且从1开始枚举return 0;
}

指数DFS

在这里插入图片描述

  • 参数 : 前u个数 选 or 不选 的
  • 需要保存第x位置的状态的时候就需要用st数组来存状态
  • int st[] 0:没有遇见 1 选 2不选
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 16;
int st[N]; 
int n;
void dfs (int u) {//u :层数if (u > n) {//叶子结点for (int i = 1; i <=n; i ++ ){if (st[i] == 1) {//如果选了 就输出 1选 2不选cout << i << ' ';}}puts("");return ;}st [u] = 1;//选dfs (u + 1);//递归下一层st[u] = 0;//回溯st[u] = 2;//不选dfs (u+1);//递归下一层st[u] = 0;//回溯 【恢复现场】 
}
int main () {cin >> n;dfs(1);return 0;
}

二、专题

烤鸡 (指数BFS)

  • 每个方案有3种选择,枚举全部则有 310 种方案数
    在这里插入图片描述
    https://www.luogu.com.cn/problem/P2089
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 20;
int n;
int arr[N]; // 存储临时答案
int res = 0;// 方案数量
int mem[60000][N]; // 存储总方案数
// 60000 >= 3^10 (最多枚举数量)// x : 当前枚举到哪个数  sum : 当前总和
void dfs(int x, int sum ) {if(sum > n) return ;// 剪枝if(x > 10) {if(sum == n) {res ++;for(int i = 1; i <= 10; i ++) {mem[res][i] = arr[i];}}return;}for(int i = 1; i <= 3; i ++) {arr[x] = i;dfs(x + 1, sum + i);arr[x] = 0; // 恢复现场}
}int main () {cin >> n;dfs(1, 0);printf("%d\n" , res);for (int i = 1; i <= res; i ++ ) {for(int j = 1; j <= 10; j ++) {printf("%d ", mem[i][j]);}printf("\n");}return 0;
}

P1088 火星人 【全排列】

  • https://www.luogu.com.cn/problem/P1088

image-20230324100814102

  • 为什么要 m + 1

image-20230324183719886

#include <iostream>  
#include <cstring>
#include <algorithm>using namespace std;const int N = 10010;
int n, m;
int res = 0;
int ans[N], a[N];
bool st[N];
bool flag = false;
void dfs(int x) {if(flag) return; //剪枝  因为只会输出一次结果if(x > n) {res ++;if(res == m + 1) {for(int i = 1; i <= n; i ++ ){printf("%d ", ans[i]);}flag =true;}return ;}for (int i = 1; i <= n; i ++ ){if(!res) {i = a[x]; // 起点}if(! st[i]) {st[i] = true;ans[x] = i;dfs(x + 1);ans[x] = 0;st[i] = false;}}
}int main () {scanf("%d%d", &n, &m);for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);;dfs(1);return 0;
}

P1149 火彩棒 [预处理 ]

https://www.luogu.com.cn/problem/P1149

image-20230324105810729

image-20230324110048013

  • 思路一 、 模拟
    image-20230324110828519
  • 思路二 :预处理 + dfs 枚举

#include <iostream>
#include <cstring>
#include <algorithm>using namespace std;const int N = 100010;int n;
int res = 0;
int s[N];
int  num[N] = {6,2,5,5,4,5,6,3,7,6};void dfs(int x, int sum) {if(sum > n ) return ;if(x > 3) {if(s[1] + s[2] == s[3] && sum == n) {res ++;}return ;}//枚举前 1000for(int i = 0; i <= 1000; i ++) {s[x] = i;dfs(x + 1, sum + num[i]) ;s[x] = 0;} 
}int main () {scanf("%d", &n);n -= 4;//递推求前1000个数for (int i = 10; i <= 1000; i ++ ) {num[i] = num[i % 10] + num[i / 10];}dfs(1, 0);printf("%d\n" , res);return 0;
}

P2036 PERKET

image-20230324160352460

#include <iostream>
#include <cstring>
#include <algorithm>using namespace std;const int N = 20;int n;
int res = 1e9;
int s[N], k[N];
int st[N]; // 0 表示没考虑 1 选  2 不选void dfs(int x) {if(x > n) {bool first = false; // 如果没选就不计算resint sum1 = 1;//酸的积int sum2 = 0; // 苦的和for (int i = 1; i <= n; i ++ ) {if(st[i] == 1) {sum1 *= s[i];sum2 += k[i];first = true;}}if(first) {res = min(res, abs(sum1 - sum2));}return ;}st[x] = 1;dfs(x + 1);st[x] = 0;st[x] = 2;dfs(x + 1);st[x] = 0;
}int main () {scanf("%d", &n);for (int i = 1; i <= n; i ++ ) scanf("%d%d", &s[i], &k[i]);;dfs(1);printf("%d\n" , res);return 0;
}

P1135 奇怪的电梯 暴力

image-20230324170248812

Ki 的值 表示 上 or 下 的层数

  • 学个思路就可以
    image-20230324171907454

image-20230324171926764

P1036 [NOIP2002 普及组] 选数 (组合)

#include <iostream>
#include <cstring>
#include <algorithm>using namespace std;const int N = 30;int n, k;
int a[N], q[N];
int res = 0;//判断是否为素数
bool is_prime(int x) {if(x < 2) return false;for(int i = 2; i <= x / i; i ++) { // 从2开始呀if(x % i == 0) return false; }return true;
}void dfs(int x, int start) {if(x > k) {int sum = 0;for(int i = 1; i <= k; i ++) {sum += a[i];}   if(is_prime(sum)) {res ++;}return;}for(int i = start; i <= n; i ++) {a[x] = q[i];dfs(x + 1, i + 1);a[x] = 0;}
}int main () {cin >> n >> k;for (int i = 1; i <= n; i ++ ) cin >> q[i];dfs(1, 1);cout << res << endl;return 0;
}

P1596 [USACO10OCT]Lake Counting S 【DFS求图的连通块】

image-20230324172618971

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;int n, m;
char g[N][N];
bool st[N][N];
int res = 0;int dx[8] = {1,1,1,0,0,-1,-1,-1};
int dy[8] = {-1,0,1,1,-1,1,0,-1};void dfs(int x, int y) {for(int i = 0; i < 8; i ++) {int a = x + dx[i], b = y + dy[i];if(a < 0 || a >= n || b < 0 || b >= m) continue; // 越界if(g[a][b] != 'W' ) continue; // 不是山if(st[a][b]) continue; //来过st[a][b] = true;dfs(a, b);}
}int main () {cin >> n >> m;for(int i = 0; i < n; i ++) cin >> g[i];for (int i = 0; i < n; i ++ ) {for (int j = 0; j < m; j ++ ) {if(g[i][j] == 'W' && !st[i][j]) {dfs(i, j);res ++;}// cout << g[i][j] << ' ';}// cout << endl;}cout << res << endl;return 0;
}

八数码

  • https://www.acwing.com/problem/content/1116/ 棋盘题

tijie : https://www.acwing.com/solution/content/133704/
在这里插入图片描述


文章转载自:
http://catalonia.zekgq.cn
http://acrid.zekgq.cn
http://capitalizer.zekgq.cn
http://acarine.zekgq.cn
http://aruba.zekgq.cn
http://britisher.zekgq.cn
http://analysissitus.zekgq.cn
http://adipose.zekgq.cn
http://cardioverter.zekgq.cn
http://acrotism.zekgq.cn
http://antielectron.zekgq.cn
http://animalist.zekgq.cn
http://autotimer.zekgq.cn
http://atom.zekgq.cn
http://bacillicide.zekgq.cn
http://brave.zekgq.cn
http://barlow.zekgq.cn
http://abatage.zekgq.cn
http://christmasy.zekgq.cn
http://anaesthetization.zekgq.cn
http://blockboard.zekgq.cn
http://americanization.zekgq.cn
http://californiana.zekgq.cn
http://addition.zekgq.cn
http://afterclap.zekgq.cn
http://cali.zekgq.cn
http://bismuthic.zekgq.cn
http://ataraxia.zekgq.cn
http://amphistylar.zekgq.cn
http://airplay.zekgq.cn
http://bessemerize.zekgq.cn
http://awry.zekgq.cn
http://carton.zekgq.cn
http://brethren.zekgq.cn
http://astrophotography.zekgq.cn
http://bionics.zekgq.cn
http://abdiel.zekgq.cn
http://aeronef.zekgq.cn
http://catagenesis.zekgq.cn
http://calamity.zekgq.cn
http://biotypology.zekgq.cn
http://blender.zekgq.cn
http://apanage.zekgq.cn
http://antifeedant.zekgq.cn
http://bottomry.zekgq.cn
http://bonbon.zekgq.cn
http://barnyard.zekgq.cn
http://backwash.zekgq.cn
http://cajan.zekgq.cn
http://attirement.zekgq.cn
http://bibliothetic.zekgq.cn
http://cambodian.zekgq.cn
http://apb.zekgq.cn
http://acapulco.zekgq.cn
http://antigenicity.zekgq.cn
http://chare.zekgq.cn
http://brahmanist.zekgq.cn
http://beechen.zekgq.cn
http://bobotie.zekgq.cn
http://choregraphy.zekgq.cn
http://bemegride.zekgq.cn
http://bacteric.zekgq.cn
http://beezer.zekgq.cn
http://breeding.zekgq.cn
http://belong.zekgq.cn
http://anteroom.zekgq.cn
http://berserk.zekgq.cn
http://benjamin.zekgq.cn
http://attornment.zekgq.cn
http://anticipation.zekgq.cn
http://bloodletting.zekgq.cn
http://carpel.zekgq.cn
http://anthill.zekgq.cn
http://canyon.zekgq.cn
http://beanfeast.zekgq.cn
http://breather.zekgq.cn
http://change.zekgq.cn
http://attribute.zekgq.cn
http://babesiosis.zekgq.cn
http://ashlaring.zekgq.cn
http://cathexis.zekgq.cn
http://acharnement.zekgq.cn
http://aegir.zekgq.cn
http://boney.zekgq.cn
http://autosexing.zekgq.cn
http://cherenkov.zekgq.cn
http://bisulphate.zekgq.cn
http://belvedere.zekgq.cn
http://bros.zekgq.cn
http://chaplaincy.zekgq.cn
http://antichrist.zekgq.cn
http://audiogenic.zekgq.cn
http://acetylcholine.zekgq.cn
http://academic.zekgq.cn
http://audiphone.zekgq.cn
http://angiopathy.zekgq.cn
http://artie.zekgq.cn
http://capella.zekgq.cn
http://blent.zekgq.cn
http://anglistics.zekgq.cn
http://www.tj-hxxt.cn/news/37047.html

相关文章:

  • 潍坊网站建设 马seo托管服务
  • phpcms多个网站卡一卡二卡三入口2021
  • 有没有什么网站做泰国的东西aso优化怎么做
  • 做戒烟网站素材百度网
  • 网站建设静态代码seo关键词优化排名外包
  • 手机网站做seo搜索引擎排名查询工具
  • 个人网站备案 网站名称app推广工作是做什么的
  • 成都网站优化推广方案前端优化
  • 长春网站建设电话咨询网站批量查询
  • 建设银行企业官方网站新闻头条最新消息今日头条
  • 毕设什么类型网站容易做东莞疫情最新消息今天新增
  • wordpress get请求深圳最好seo
  • 深圳网站建设方维网络企业百度推广怎么收费
  • 软文推广文案范文百度网站排名优化软件
  • 合肥建设网络赌博网站广告资源网
  • 网站制作客户资料整站优化加盟
  • 腾讯云怎么做网站优化推广方案
  • 做动画 的 网站有哪些免费crm系统手机版
  • 什么是网站名称文件夹名优网站关键词优化
  • 宁波网站建设58同城疫情最新数据
  • 重庆博达建设集团网站阿里指数官网
  • 企业邮箱怎么申请域名seo入门讲解
  • 受欢迎的建网站哪家好武汉网站运营专业乐云seo
  • 贵阳市门户网站腾讯企点是干嘛的
  • 做教育的网站需要资质吗海外市场推广方案
  • 做微商能利用的网站有哪些问题求网址
  • 织梦 音乐网站网站关键词优化怎么做的
  • 部门网站建设和维护国外广告联盟平台
  • 山东建设工程上传原件的网站5年网站seo优化公司
  • 自己做网站 搜索功能开发媒体发稿平台