Java 回溯法算法演示代码
package com.lh.DataConsutror; public class BacktrackTest { static int [] array = new int[3]; static boolean [] flag = new boolean[3]; public static void backTrack(int length) { int len = array.length; if(length == len) { for(int i = 0; i < array.length; i++) { System.out.print(array[i]); } System.out.println(); } for(int j = 0; j < len; j++) { if(flag[j] == false) { array[j] = j + 1; flag[j] = true; backTrack(length + 1); flag[j] = false; } } } public static void main(String[] args) { backTrack(0); } }