1 Star 0 Fork 1

hanjun/Aha-Algorithms-practise

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
4.1.1深度优先搜索-1~9全排列.cpp 425 Bytes
一键复制 编辑 原始数据 按行查看 历史
柳婼 提交于 2016-02-19 12:48 . rename
4.1 深度优先搜索
全排列 123456789 数字
#include <iostream>
using namespace std;
int book[10];
int a[10];
int n;
void dfs(int step) {
if (step == n + 1) {
for (int i = 1; i <= n; i++) {
cout << a[i];
}
cout << endl;
return;
}
for (int i = 1; i <= n; i++) {
if (book[i] == 0) {
a[step] = i;
book[i] = 1;
dfs(step + 1);
book[i] = 0;
}
}
}
int main() {
cin >> n;
dfs(1);
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/digua_ge/Aha-Algorithms-practise.git
git@gitee.com:digua_ge/Aha-Algorithms-practise.git
digua_ge
Aha-Algorithms-practise
Aha-Algorithms-practise
master

搜索帮助