ȫ����
c++����
#include<iostream>
using namespace std;
const int N = 10;
int path[N];
bool st[N] = { 0 };
int n;
void dfs(int u)
{
if (u > n) {
for (int i = 1; i <= n; i++) {
printf("%d ", path[i]);
}
puts("");
}
else {
for (int i = 1; i <= n; i++) {
if (!st[i]) {
st[i] = true;
path[u] = i;
dfs(u + 1);
st[i] = false;
}
}
}
}
int main() {
cin >> n;
dfs(1);
return 0;
}
java����
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
public class Main {
private static int[] path;
private static boolean[] st;
private static int n;
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
private static void dfs(int u) throws Exception {
if (u == n) {
for (int i = 0; i < n; i ++ )
bw.write(path[i] + 1 + " ");
bw.write("\n");
} else {
for (int i = 0; i < n; i ++ )
if (!st[i]) {
path[u] = i;
st[i] = true;
dfs(u + 1);
st[i] = false; // �ָ��ֳ�
}
}
}
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
path = new int[n];
st = new boolean[n];
dfs(0);
bw.flush();
}
}