题目:给定一个没有重复数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
分析:利用树形结构 枚举出所有可行解集合,然后利用剪枝方法 得到符合某一约束规则的可行解。 利用回溯法实现。
回溯法的基本思想是先递归,然后回溯,
当回溯到树形结构中的某一节点时,代码是如何记住每一步索引的呢?
个人理解是因为递归是利用栈分配空间的,所以每一次回溯时,其栈空间会记录前面递归时的索引值。
回溯的具体细节用文字不好表述,可以打印查看,自行理解。
/*
回溯法 全排列
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
*/
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
int data[3]={1,2,3};
//vector<int> data={1,2,3};
vector< int> vis(3,0);//记录是否已经访问
vector< vector <int> > res;
vector< int >v(3,0);
void dfs(int* nums, int index)
{
/*
问题:回到获取指针指向数组的长度这个问题,
是不能直接得到的(有什么奇技淫巧就不晓得了)。建议用 std::array 或 std::vector
vector<int> temp=nums;
cout<<sizeof(nums);///sizeof(nums[0]);
for(int i=0;i<sizeof(nums)/sizeof(nums[0]);++i)
cout<<nums[i];
*/
// int countChar=0;
// int*temp=nums;
// while(*temp){
// cout<<*temp;
// temp++;
// }
//
if(index>3)return;
if(index==3){
res.push_back(v);
for(int i=0;i<v.size();i++)
cout<<"v"<<i<<':'<<v[i]<<',';
cout<<"\n";
// printf("",res) ;
// cout<<res[0];
return;
}
for(int i=0;i<v.size();i++)
cout<<"v"<<i<<':'<<v[i]<<',';
cout<<"\n";
//遍历方案
for(int i=0;i<3;i++)
{
if(!vis[i])//当前没有被访问,
{ vis[i]=1;
v[index]=nums[i];
//res.push_back(nums[i]);
// cout<<"dfs_b"<<index<<"\n";
dfs(data,index+1);
vis[i]=0;
v[index]=-100;
for(int i=0;i<v.size();i++)
cout<<"v"<<i<<':'<<v[i]<<',';
cout<<"\n";
// cout<<"dfs_a"<<index<<'\n';
}
}
}
int main(void)
{
//输出每个数组
int dataLeng=sizeof(data)/sizeof(data[0]);//来获取数组的长度。;
cout<<dataLeng;
for(int i=0;i<dataLeng;i++)
{
cout<<data[i];
}
cout<<"\n";
//int*temp=data;
//while(*temp){
//cout<<*temp;
//temp++;
//}
dfs(data,0) ;
int m=res.size();
int n=res[0].size();
int count=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
count++;
cout<<res[i][j];
if(count%3==0)cout<<"\n";
}
}
return 1;
}
输出结果:
3123
v0:0,v1:0,v2:0,
v0:1,v1:0,v2:0,
v0:1,v1:2,v2:0,
v0:1,v1:2,v2:3,
v0:1,v1:2,v2:-100,
v0:1,v1:-100,v2:-100,
v0:1,v1:3,v2:-100,
v0:1,v1:3,v2:2,
v0:1,v1:3,v2:-100,
v0:1,v1:-100,v2:-100,
v0:-100,v1:-100,v2:-100,
v0:2,v1:-100,v2:-100,
v0:2,v1:1,v2:-100,
v0:2,v1:1,v2:3,
v0:2,v1:1,v2:-100,
v0:2,v1:-100,v2:-100,
v0:2,v1:3,v2:-100,
v0:2,v1:3,v2:1,
v0:2,v1:3,v2:-100,
v0:2,v1:-100,v2:-100,
v0:-100,v1:-100,v2:-100,
v0:3,v1:-100,v2:-100,
v0:3,v1:1,v2:-100,
v0:3,v1:1,v2:2,
v0:3,v1:1,v2:-100,
v0:3,v1:-100,v2:-100,
v0:3,v1:2,v2:-100,
v0:3,v1:2,v2:1,
v0:3,v1:2,v2:-100,
v0:3,v1:-100,v2:-100,
v0:-100,v1:-100,v2:-100,
123
132
213
231
312
321
--------------------------------
Process exited after 0.7675 seconds with return value 1
请按任意键继续. . .
如果不进行剪枝,求所有可能的组合情况,则把约束条件去掉即可,结果如下:
3123
v0:0,v1:0,v2:0,
v0:1,v1:0,v2:0,
v0:1,v1:1,v2:0,
v0:1,v1:1,v2:1,
v0:1,v1:1,v2:-100,
v0:1,v1:1,v2:2,
v0:1,v1:1,v2:-100,
v0:1,v1:1,v2:3,
v0:1,v1:1,v2:-100,
v0:1,v1:-100,v2:-100,
v0:1,v1:2,v2:-100,
v0:1,v1:2,v2:1,
v0:1,v1:2,v2:-100,
v0:1,v1:2,v2:2,
v0:1,v1:2,v2:-100,
v0:1,v1:2,v2:3,
v0:1,v1:2,v2:-100,
v0:1,v1:-100,v2:-100,
v0:1,v1:3,v2:-100,
v0:1,v1:3,v2:1,
v0:1,v1:3,v2:-100,
v0:1,v1:3,v2:2,
v0:1,v1:3,v2:-100,
v0:1,v1:3,v2:3,
v0:1,v1:3,v2:-100,
v0:1,v1:-100,v2:-100,
v0:-100,v1:-100,v2:-100,
v0:2,v1:-100,v2:-100,
v0:2,v1:1,v2:-100,
v0:2,v1:1,v2:1,
v0:2,v1:1,v2:-100,
v0:2,v1:1,v2:2,
v0:2,v1:1,v2:-100,
v0:2,v1:1,v2:3,
v0:2,v1:1,v2:-100,
v0:2,v1:-100,v2:-100,
v0:2,v1:2,v2:-100,
v0:2,v1:2,v2:1,
v0:2,v1:2,v2:-100,
v0:2,v1:2,v2:2,
v0:2,v1:2,v2:-100,
v0:2,v1:2,v2:3,
v0:2,v1:2,v2:-100,
v0:2,v1:-100,v2:-100,
v0:2,v1:3,v2:-100,
v0:2,v1:3,v2:1,
v0:2,v1:3,v2:-100,
v0:2,v1:3,v2:2,
v0:2,v1:3,v2:-100,
v0:2,v1:3,v2:3,
v0:2,v1:3,v2:-100,
v0:2,v1:-100,v2:-100,
v0:-100,v1:-100,v2:-100,
v0:3,v1:-100,v2:-100,
v0:3,v1:1,v2:-100,
v0:3,v1:1,v2:1,
v0:3,v1:1,v2:-100,
v0:3,v1:1,v2:2,
v0:3,v1:1,v2:-100,
v0:3,v1:1,v2:3,
v0:3,v1:1,v2:-100,
v0:3,v1:-100,v2:-100,
v0:3,v1:2,v2:-100,
v0:3,v1:2,v2:1,
v0:3,v1:2,v2:-100,
v0:3,v1:2,v2:2,
v0:3,v1:2,v2:-100,
v0:3,v1:2,v2:3,
v0:3,v1:2,v2:-100,
v0:3,v1:-100,v2:-100,
v0:3,v1:3,v2:-100,
v0:3,v1:3,v2:1,
v0:3,v1:3,v2:-100,
v0:3,v1:3,v2:2,
v0:3,v1:3,v2:-100,
v0:3,v1:3,v2:3,
v0:3,v1:3,v2:-100,
v0:3,v1:-100,v2:-100,
v0:-100,v1:-100,v2:-100,
111
112
113
121
122
123
131
132
133
211
212
213
221
222
223
231
232
233
311
312
313
321
322
323
331
332
333
--------------------------------
Process exited after 0.5446 seconds with return value 1
请按任意键继续. . .