抓取法全排列
public class GrabArray {
static int[] visit = new int[3];
public static void main(String[] args) {
char c[] = {'a','b','c'};
char[] b = new char[3];
f(0,c,b);
}
static void f(int k,char c[],char b[]){
if(k == 3){ //出口
for(int i = 0;i < 3;i++){ //输出这个排列
System.out.print(b[i]+" ");;
}
System.out.println();
}
for(int i = 0;i < 3;i++){
if(i > 0 && c[i-1] == c[i] && visit[i-1] == 0){ //规则
continue;
}
if(visit[i] == 0){ //当前元素未被抓取
visit[i] = 1; //设置为已抓取
b[k] = c[i]; //放入临时数组中
f(k+1,c,b); //递归
visit[i] = 0; //回溯
}
}
}
}