import java.util.Scanner;
public class Main{
static int N = 10;
static int n,u; //n表示一共要走多少步,u表示当前在第几步
static int[] path = new int[N]; //path存储路径
static boolean[] st = new boolean[N]; //表示当前路是否已经走过,true表示走过,false表示没有
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
n = in.nextInt();
dfs(0);
}
private static void dfs(int u)
{
if(u == n) //如果走到了头,就输出结果
{
for(int i = 0; i < n; i ++) System.out.print(path[i] + " ");
System.out.println();
}
else
{
for(int i = 1; i <= n; i ++) //循环的是值
{
if(!st[i]) //如果还没有走过的话
{
path[u] = i;
st[i] = true;
dfs(u + 1);
st[i] = false; //恢复现场
}
}
}
}
}
DFS搜索
最新推荐文章于 2024-06-17 11:09:28 发布