HDOJ 1016 프 라 임 링 문제 [심층 검색]
Note: the number of first circle should always be 1.
Input n (0 < n < 20).
Output The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
Sample Input 6 8
Sample Output Case 1: 1 4 3 2 5 6 1 6 5 2 3 4
Case 2: 1 2 3 8 5 6 7 4 1 2 5 8 3 4 7 6 1 4 7 6 5 8 3 2 1 6 7 4 3 8 5 2
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int Case = 0;
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int a[] = new int[n];
            //    1-n
            int color[] = new int[n];
            //          
            int prant[] = new int[n];
            //      
            int count =0;//   
            for(int i=0;i<n;i++){
                a[i]=i+1;
                color[i] = -1;
            }//     
            Case++;
            System.out.println("Case "+(Case)+":");
            dfs(a,color,prant,count,0);
            System.out.println();
        }
    }
    private static void dfs(int[] a, int[] color, int[] prant, int count,int m) {
        //System.out.println(count);
        count++;//    1
        if(count == a.length&&p(prant[0],a[m])){
        //                      
            prant[count-1]=a[m];
            for(int i=0;i<a.length-1;i++){
                System.out.print(prant[i]+" ");
            }
            System.out.println(prant[a.length-1]);
            //return ;
        }
        for(int i=0;i<a.length;i++){
            color[m] =1;
            if(p(a[m],a[i])&&color[i]==-1){
                color[i]=1;
                prant[count-1]=a[m];
                dfs(a,color,prant,count,i);
                color[i]=-1;
            }
        }
    }
//       
    private static boolean p(int i, int j) {
        int sum = i+j;
        for(int a=2;a*a<=sum;a++){
            if(sum%a==0){
                return false;
            }
        }
        return true;
    }
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.