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

DFS入门刷题c++

目录

821. 跳台阶 - AcWing题库

​92. 递归实现指数型枚举 - AcWing题库 

​P1706 全排列问题 - 洛谷 (luogu.com.cn)

P1157 组合的输出 - 洛谷 (luogu.com.cn)

​P1036 [NOIP 2002 普及组] 选数 - 洛谷 (luogu.com.cn)

P2089 烤鸡 - 洛谷 (luogu.com.cn)

P1088 [NOIP 2004 普及组] 火星人 - 洛谷 (luogu.com.cn)


 821. 跳台阶 - AcWing题库

#include<iostream>
using namespace std;
int t(int n) {if (n <= 2)return n;if (n >= 3)return t(n - 1) + t(n - 2);
}
int main() {int n; cin >> n;cout << t(n);return 0;
}

92. 递归实现指数型枚举 - AcWing题库 

#include<iostream>
using namespace std;
int n;
int s[20];//0表示未考虑;1表示选;2表示不选
void dfs(int x) {if (x > n) {for (int i = 1; i <= n; i++) {if (s[i] == 1)cout << i << " ";}cout << endl;return;}for (int i = 1; i <= 2; i++) {s[x] = i;dfs(x + 1);}
}
int main() {cin >> n;dfs(1);return 0;
}

 P1706 全排列问题 - 洛谷 (luogu.com.cn)

#include<iostream>
using namespace std;
int n;
int s[20];
bool vis[20];
void dfs(int x) {if (x > n) {for (int i = 1; i <= n; i++) {printf("%5d", s[i]);}cout << endl;return;}for (int i = 1; i <= n; i++) {if (!vis[i]) {vis[i] = true;s[x] = i;dfs(x + 1);vis[i] = false;s[x] = 0;}}
}
int main() {cin >> n;dfs(1);return 0;
}

 P1157 组合的输出 - 洛谷 (luogu.com.cn)

#include<iostream>
using namespace std;
int n, r;
int a[25];
void dfs(int x, int start) {if (x > r) {for (int i = 1; i <= r; i++) {printf("%3d", a[i]);}cout << endl;return;}for (int i = start; i <= n; i++) {a[x] = i;dfs(x + 1, i + 1);a[x] = 0;}
}
int main() {cin >> n >> r;dfs(1,1);return 0;
}

 P1036 [NOIP 2002 普及组] 选数 - 洛谷 (luogu.com.cn)

#include<iostream>
using namespace std;
int n, k;
int a[25]; int s[25];
int res;
bool jud(int t) {if (t < 2)return false;for (int i = 2; i * i <= t; i++) {if (t % 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 += s[i];}if (jud(sum))res++;return;}for (int i = start; i <= n; i++) {s[x] = a[i];dfs(x + 1, i + 1);s[x] = 0;}
}
int main() {cin >> n >> k;for (int i = 1; i <= n; i++)cin >> a[i];dfs(1, 1);cout << res;return 0;
}

P2089 烤鸡 - 洛谷 (luogu.com.cn)

#include<iostream>
using namespace std;
int n;
int res;
int a[60000][11];
int tem[11];
void dfs(int x, int sum) {if (sum > n)return;if (x > 10) {if (sum == n) {res++;for (int i = 1; i <= 10; i++) {a[res][i] = tem[i];}}return;}for (int i = 1; i <= 3; i++) {tem[x] = i;dfs(x + 1, sum + i);}
}
int main() {cin >> n;dfs(1, 0);cout << res << endl;for (int i = 1; i <= res; i++) {for (int j = 1; j <= 10; j++) {cout << a[i][j] << " ";}cout << endl;}return 0;
}

 P1088 [NOIP 2004 普及组] 火星人 - 洛谷 (luogu.com.cn)

#include<iostream>
using namespace std;
int n, m;
int res;
const int N = 10010;
int a[N]; bool vis[N]; int mars[N];
void dfs(int x) {if (x > n) {res++;if (res == m + 1) {for (int i = 1; i <= n; i++) {cout << a[i] << " ";}exit(0);//剪枝,输出之后强制退出}}for (int i = 1; i <= n; i++) {if (!res) {i = mars[x];}if (!vis[i]) {vis[i] = true;a[x] = i;dfs(x + 1);vis[i] = false;a[x] = 0;}}
}
int main() {cin >> n >> m;for (int i = 1; i <= n; i++)cin >> mars[i];dfs(1);return 0;
}


文章转载自:

http://iUIibCb2.mqfhy.cn
http://xdACXKu4.mqfhy.cn
http://wPBEL0cr.mqfhy.cn
http://r3cRtnTX.mqfhy.cn
http://Rdr2SuM6.mqfhy.cn
http://Q9NaAuDI.mqfhy.cn
http://3U1LvBH6.mqfhy.cn
http://71AmTJXh.mqfhy.cn
http://ONBFtD0P.mqfhy.cn
http://qhAfLbUu.mqfhy.cn
http://5bKjX0O4.mqfhy.cn
http://LZ8sOEnx.mqfhy.cn
http://jQjCdxJz.mqfhy.cn
http://RABpd8O1.mqfhy.cn
http://jUdJTASY.mqfhy.cn
http://7EHRlf6l.mqfhy.cn
http://dI1WyKuQ.mqfhy.cn
http://RGInrVuE.mqfhy.cn
http://B6oaTtvw.mqfhy.cn
http://Zkx94ewI.mqfhy.cn
http://VOTlb1Ti.mqfhy.cn
http://yLMNZRQh.mqfhy.cn
http://WGm1Rbei.mqfhy.cn
http://yLH25QED.mqfhy.cn
http://mHdoKHfX.mqfhy.cn
http://Jcris0QG.mqfhy.cn
http://bK5Dbsar.mqfhy.cn
http://aLuUGOwv.mqfhy.cn
http://2dsbwSxb.mqfhy.cn
http://HpFy0aWN.mqfhy.cn
http://www.dtcms.com/a/217380.html

相关文章:

  • AI 智能体的那些事—架构设计关键点
  • 数据库管理与高可用-MySQL数据库初体验
  • Java 内存模型与 volatile 关键字深度解析:从可见性到指令重排
  • 【键盘说明书备份】ENERGYFORT
  • 什么是舵机,如何控制舵机
  • LVGL(Grid)
  • 用Qt/C++玩转观察者模式:一个会聊天的设计模式
  • Baklib企业CMS实现内容智能归档与精准检索
  • 红黑树,B树,二叉树之间的不同
  • C++类继承详解:权限控制与继承方式解析
  • Gemini Pro 2.5 输出
  • AI 编程如何让你轻松采集网站数据?
  • 第二十一章:数据治理之数据安全:数据安全的驱动因素以及常见的数据安全举措
  • 阿姆斯特朗数
  • 五大要素协同效益的量化模型与实战策略
  • 【Qt开发】容器类控件
  • 真话与假话
  • Java集合框架详解:List、Set、Map及其实现类
  • C-内存函数,动态内存
  • 人工智能概念股:最新投资机会深度解析
  • 数字人教师:开启教育智慧革新之旅
  • 02_MQ常见问题
  • 网络编程--上篇
  • Minktec 柔性弯曲传感器,灵敏捕捉坐姿弓背、精准监测行走姿态,守护儿童背部健康,为科学健身提供数据支撑,开启职业健康与背痛 AI 干预新方向。
  • 将图层为shapefile类型的文件转成PostGis类型的详细实现步骤
  • java每日精进 5.27【异步实现】
  • SQL计算列
  • vue展示修改前后对比,并显示修改标注diff
  • YOLOv2 深度解析:目标检测领域的进阶之路
  • 借教室--二分+查分