94. 递归实现排列型枚举

 

 

#include <iostream>
#include <vector>
using namespace std;

int n;
vector<int> path;

void dfs(int u, int state)
{
	if(u == n)
	{
		for(auto x : path)	cout << x << " ";
		cout << endl;
		return ;
	}
	
	for(int i = 0; i < n; ++ i)
	{
		if(!(state >> i & 1))
		{
			path.push_back(i + 1);
			dfs(u + 1, state | 1 << i);
			path.pop_back();
		}
	}
}

int main()
{
	cin >> n;
	dfs(0, 0);
	return 0;
} 

  

posted @ 2019-10-27 21:14  青衫客36  阅读(147)  评论(0编辑  收藏  举报