dfs——n的全排列(回溯)
#include <iostream>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const double Pi=3.14159265358979323846;
typedef long long ll;
const int MAXN=+;
const int dx[]={,,,,-};
const int dy[]={,-,,,};
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll mod=1e9+;
int n;
int flag[MAXN],a[MAXN];
int cnt=;
int t; void dfs(int t)
{
if(t==)
{
for(int i=;i<=n;i++)
{
cout << a[i]<<" ";
}
cout <<endl;
return;
}
for(int i=;i<=n;i++)
{
if(flag[i]==)
{
flag[i]=;
a[cnt++]=i;
//cout <<a[cnt-1]<<" ";
dfs(t-);
cnt--;flag[i]=;
} }
}
int main()
{
while(scanf("%d",&n)&&n)
{
t=n;
dfs(t);
}
return ;
}
dfs——n的全排列(回溯)的更多相关文章
- LeetCode刷题总结-DFS、BFS和回溯法篇
本文总结LeetCode上有关深度优先搜索(DFS).广度优先搜索(BFS)和回溯法的算法题,推荐刷题总数为13道.具体考点分析如下图: 一.深度优先搜索 1.字符匹配问题 题号:301. 删除无效的 ...
- [LeetCode] 46. Int数组全排列 ☆☆☆(回溯)
描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2, ...
- [LeetCode] 46. 全排列(回溯)
###题目 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], ...
- [leetcode] 39. 组合总和(Java)(dfs、递归、回溯)
39. 组合总和 直接暴力思路,用dfs+回溯枚举所有可能组合情况.难点在于每个数可取无数次. 我的枚举思路是: 外层枚举答案数组的长度,即枚举解中的数字个数,从1个开始,到target/ min(c ...
- caioj 1031: [视频]递归1(全排列)【DFS】【全排列】
题目大意:先给一个正整数 n( 1 < = n < = 10 ),输出1到n的所有全排列. 题解:这道题目我们可以用递归来实现,递归在图论中又称为"深度优先搜索"(De ...
- dfs——皇后问题(回溯)
#include <iostream> using namespace std; ],b[],c[],d[]; ; dfs(int i) { if(i>n) { sum++; ) { ...
- poj The Settlers of Catan( 求图中的最长路 小数据量 暴力dfs搜索(递归回溯))
The Settlers of Catan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1123 Accepted: ...
- [LeetCode] 784. 字母大小写全排列 ☆☆☆(回溯、深度优先遍历)
https://leetcode-cn.com/problems/letter-case-permutation/solution/shen-du-you-xian-bian-li-hui-su-su ...
- Permutations 全排列 回溯
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
随机推荐
- Android 之常用布局
LinearLayout 线性布局. android:orientation="horizontal" 制定线性布局的排列方式 水平 horizontal 垂直 vertical ...
- Spring事务的开启方式
1.通过注解方式@Transactional @Transactional(rollbackForClassName = { "Exception", "RuntimeE ...
- Values & Reference:值和引用
var a = 2; var b = a; //b 是 a 的值的一个副本 b++; a; b; var c = [1, 2, 3]; var d = c; // d 是 值[1, 2, 3]的一个引 ...
- http协议与浏览器缓存
http协议与浏览器缓存 F5刷新与在地址栏回车的区别 链接
- EF-生成迁移版本
前面讲到可以使用迁移技术让程序自动更新数据库中相关的结构.在我们每次需要新增模型类时,请一定要养成一个好的习惯,使用Add-Migration命令生成迁移版本.这样能恢复被误删除的表. 一.新增迁移版 ...
- x多进程
#!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' from multiprocessing import Process import os #子进 ...
- C++基础知识:构造与析构
1.构造函数的定义: C++中的类可以定义与类名相同的特殊成员函数这种与类名相同的成员函数叫做构造函数构造函数在定义时可以有参数,但是没有任何返回类型的声明 2.构造函数的调用: 一般情况下C++编译 ...
- 2.14 C++析构函数
参考: http://www.weixueyuan.net/view/6345.html 总结: 析构函数就是用于回收创建对象时所消耗的各种资源. 析构函数的调用顺序与构造函数调用顺序正好是相反的. ...
- ubuntu14.04下搜狗输入法不能输入中文问题解决
解决方法如下: 一.重启搜狗输入法 通过下面的两个命令重启搜狗输入法 ~$ killall fcitx ~$ killall sogou-qinpanel 二.检查修复安装依赖 ~$ sud ...
- Join Algorithm
Join(SQL) An SQL join clause combines columns from one or more tables in a relational database. It c ...