题目描述
blablabla
样例
blablabla
blablabla
时间复杂度分析:blablabla
C++ 代码
#include<iostream>
#include<vector>
using namespace std;
const int N = 10000;
int n, p[N];
//vector<int> p;
bool s[N];
void dfs(int u, int state)
{
if(u == n)
{ for(int i = 0; i < n; i++)
cout << p[i] << " ";
cout << endl;
}
else
{
for(int i = 1; i <= n; i++)
if( !(state >> i & 1)) //第i位没有被选中
{
p[u] = i;
dfs(u+1, 1 << i | state); //把第i位标记为选中
}
}
}
int main()
{
cin >> n;
dfs(0, 0);
return 0;
}
#include<iostream>
using namespace std;
const int N = 10000;
int n, p[N];
bool s[N];
void dfs(int u)
{
if(u == n)
{ for(int i = 0; i < n; i++)
cout << p[i] << " ";
cout << endl;
}
else
{
for(int i = 1; i <= n; i++)
if( !s[i])
{
p[u] = i; s[i] = true;
dfs(u+1 ); s[i] = false;
}
}
}
int main()
{
cin >> n;
dfs(0);
return 0;
}