2016-11-07 1 views
1

이상한 위치 n의 모든 점이 위치 (n-1)의 점 앞에 나타날 수 없다는 제한하에 java의 점 집합을 바꾸려고합니다. 즉, 2 점 1과 2가 주어지면 2가 1보다 먼저 나타날 수 없습니다 순열과 주어진 점 1,2,3 & 4의에서 예상 순열의 설정은 다음과 같습니다제한이있는 점 집합을 어떻게 바꾸는가?

1,2,3,4 
1,3,2,4 
1,3,4,2 
3,1,2,4 
3,4,1,2 
3,1,4,2 
나는 현재 다음 찾는 순열에 대한 코드가

:

static void permute(int[] a, int k,int[] p) { 
    if (k == a.length) { 
     for (int i = 0; i < a.length; i++) { 
      System.out.print(" " + a[i]); 
     } 
     System.out.println(); 
    } 
    else { 
     int temp; 
     for (int i = k; i < a.length; i++) { 
      if(i % 2 == 0){ 
       temp = a[k]; 
       a[k] = a[i]; 
       a[i] = temp; 
       permute(a, k + 1,p); 
       temp = a[k]; 
       a[k] = a[i]; 
       a[i] = temp; 
      } 
      else{ 
       if(k > p[i]){ 
        temp = a[k]; 
        a[k] = a[i]; 
        a[i] = temp; 
        permute(a, k + 1,p); 
        temp = a[k]; 
        a[k] = a[i]; 
        a[i] = temp; 
       } 
      } 
     } 
    } 
} 

하지만 내 전류를 출력 :

1 2 3 4 
1 2 4 3 
1 3 2 4 
1 3 4 2 
1 4 3 2 
1 4 2 3 
3 2 1 4 
3 2 4 1 
3 1 2 4 
3 1 4 2 
3 4 1 2 
3 4 2 1 

어떤 도움이 정말 재귀 적 접근 방식에 대해

+0

이러한 종류의 연습 문제는 잘 문서화되어 있으며 모두 역 추적을 적용하여 해결할 수 있습니다. 이 http://www.fas.harvard.edu/~cscie119/lectures/recursion.pdf를 살펴볼 수 있습니다. 18 페이지의 백 트랙킹 알고리즘 템플릿은 모두 동일하게 보입니다. 더 일반화 된 버전은 노드에서 데이터를 랩하여 재사용을 쉽게합니다. –

+0

규칙을보다 정확하게 설명 할 수 있습니까? '홀수 위치 n의 모든 점들이 위치 (n-1)의 지점보다 먼저 나타날 수 없다면'홀수 위치 (3)에있는 3이 어떻게 2 이전에 나타날 수 있습니까? 왜 규칙이 '1'에만 적용되고 예를 들어 숫자가 1,2,3,4입니까? – MrSmith42

+0

@ MrSmith42 : 그는 "even"을 의미했을 수도 있습니다. 따라서 2가 1보다 앞에 나오지 않고 4가 3보다 앞에 나타날 수 없습니다. –

답변

1

무엇 :-) 주시면 감사하겠습니다?

조건이 위반되는 즉시 재귀 분기를 차단하십시오.

2

먼저 모든 순열을 찾은 다음 제한이 존중되는 필터 만 필터링 할 수 있습니다. 아래 예 :

import java.util.ArrayList; 
import java.util.List; 

public class PermutationsExample { 

    static int[] arr = {1,2,3,4}; 
    public static void main(String[] args) { 
     List<List<Integer>> allPermutationList = getAllPermutations(arr); 
     System.out.println("All permutations are :"); 
     System.out.println(allPermutationList); 
     System.out.println(""); 

     List<List<Integer>> subPermutationList = getRestrictedPermutations(allPermutationList); 
     System.out.println("Permutations with restrictions are:"); 
     System.out.println(subPermutationList); 
    } 

    // see http://www.programcreek.com/2013/02/leetcode-permutations-java/ for further info 

    public static List<List<Integer>> getAllPermutations(int[] num) { 
     List<List<Integer>> result = new ArrayList<>(); 
     result.add(new ArrayList<>()); 
     for (int i = 0; i < num.length; i++) { 
      List<List<Integer>> current = new ArrayList<>(); 
       for (List<Integer> l : result) { 
        for (int j = 0; j < l.size()+1; j++) { 
         l.add(j, num[i]); 
         List<Integer> temp = new ArrayList<>(l); 
         current.add(temp); 
         l.remove(j); 
        } 
       } 
      result = new ArrayList<>(current); 
     } 
     return result; 
    } 

    public static List<List<Integer>> getRestrictedPermutations(List<List<Integer>> listofList){ 
     List<List<Integer>> result = new ArrayList<>(); 
     for(List<Integer> list: listofList){      
      if(isRestrictionRespected(list)){ 
       result.add(list); 
      }    
     }   
     return result; 
    } 

    public static boolean isRestrictionRespected(List<Integer> list){ 
     boolean result = true; 
     for (int i = 1; i < arr.length; i+=2) {    
       if(list.indexOf(arr[i])<list.indexOf(arr[i-1])){ 
       result = false; 
       break; 
       } 
      } 
     return result; 
    } 
} 
관련 문제