数字全排列 java深度优先搜索

 1 import java.util.Scanner;
 2 import java.util.ArrayList;
 3 public class Quanpailie{
 4     
 5     public int a[] ;
 6     public int book[] ;
 7     public int n;
 8     public int step=1;
 9     public static void main(String[] args) {
10         Quanpailie he=new Quanpailie();
11         he.go();
12         //深度搜索;
13     }
14     public void setup(int step){
15         for(int i=1;i<=n;i++){
16             if(step==n+1){
17                 look(a);
18                 break;
19             }else{
20                 if(book[i]==0){
21                     a[step]=i;
22                     book[i]=1;
23                     setup(step+1);
24                     book[i]=0;
25                 }
26             }
27         }
28     }
29     public void look(int[] list){
30         System.out.println("");
31         for(int i=1;i<list.length;i++){
32             System.out.print(list[i]);
33         }
34     }
35 
36     public void go(){
37         Scanner sc=new Scanner(System.in);
38         n=sc.nextInt();
39         a=new int[n+1];
40         book=new int[n+1];
41         setup(1);
42     }
43     
44 }

 

posted on 2015-12-15 08:40  shortail  阅读(446)  评论(0编辑  收藏  举报