forked from Jyo561/Backtracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permutations.java
55 lines (55 loc) · 1.05 KB
/
permutations.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class perm{
static void Backtracking(int a[],int k,int n,int input[]){
int c[]=new int[n];
int ncan=0;
if(is_a_solution(n,k)){
process_solution(a,n,k,input);
}
else{
k++;
ncan=construct_candidate(a,c,k-1,n,input);
for(int i=0;i<ncan;i++) {
a[k-1]=c[i];
Backtracking(a,k,n,input);
}
}
}
static int construct_candidate(int a[],int c[],int k,int n,int input[]){
int i=0;
int ncan=0;
int in_perm[]=new int[6];
for ( i=1;i<6;i++) {
in_perm[i]=0;
}
for(i=0;i<k;i++){
in_perm[a[i]]=1;
}
for (int j=1;j<=n;j++ ) {
if(in_perm[input[j-1]]==0){
c[ncan]=input[j-1];
ncan+=1;
}
}
return ncan;
}
static boolean is_a_solution(int k,int n){
if((k)==(n)){
return true;
}
else{
return false;
}
}
static void process_solution(int a[],int n,int k,int input[]){
for (int i=0;i<k;i++) {
System.out.print(a[i]);
}
System.out.println();
}
public static void main(String[] args) {
int input[]={2,5,4};
int n=input.length;
int a[]=new int[n];
Backtracking(a,0,n,input);
}
}