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 ...
随机推荐
- Java类型中ParameterizedType,GenericArrayType,TypeVariabl,WildcardType详解
(1). 和反射+泛型有关的接口类型 java.lang.reflect.Type:java语言中所有类型的公共父接口 java.lang.reflect.ParameterizedType java ...
- 模块之 logging, shelve, sys 模块
一. logging模块 用来记录日志,日志:记录某个时间点发生了什么事 日志作用:程序调试 了解软件程序的运行情况,是否正常 软件程序运行故障分析与问题定位 还可用来做用户行为分析 日志等级:在不改 ...
- android studio maven 仓库的使用
转自:http://www.cnblogs.com/sihaixuan/p/4852974.html 原文:How to distribute your own Android library thr ...
- python3.7 安装
python3.7 安装 下载安装 cd /usr/localwget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgztar -xvf ...
- 在shell脚本里执行sudo 命令
可以 : echo "yourpasswd" |sudo -S yourcommand 但是不安全,因为密码都显示在shell脚本里面了-_- 引自http://hi.baid ...
- 用linux命令连接无线网络-转载
首先是用到的工具: ifconfigrouteiwlistiwconfig 后两个是无线工具 从现在开始,按我的步骤做 (##后面的是说明部分) 1.开启无线,如果是笔记本,开启无线开关,或用Fn+F ...
- 摄像头录制视频并且保存成mp4
import cv2import numpy as npimport os cap = cv2.VideoCapture(1)#v4l2-ctl --list-devices 查看设备号,非正常中断时 ...
- java获得当前系统时间三种方法
参见: http://blog.csdn.net/cloume/article/details/46624637
- linux:centOs7换源阿里云
备份: mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 下载: wget -O /etc/y ...
- vue-router-8-路由组件传参
在组件中使用$route会使之与其对应路由形成高度耦合,使用props解耦 const User = { props: ['id'], template: '<div>User{{ id ...