less than 1 minute read

문제 풀이 방식

문제 풀이 (Java)

import java.io.BufferedReader;  
import java.io.InputStreamReader;  
import java.util.StringTokenizer;  
  
public class boj10974 {  
    static int N;  
    static int[] foo;  
    static boolean[] visit;  
    public static void main(String[] args) throws Exception {  
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
        StringTokenizer st = new StringTokenizer(br.readLine());  
  
        N = Integer.parseInt(st.nextToken());  
  
        foo = new int[N];  
        visit = new boolean[N];  
  
        DFS(0);  
    }  
  
    public static void DFS(int depth){  
        if (depth == N){  
            // 탐색 종료 조건 : 길이가 N            printArray(foo);  
            return;  
        }  
  
        // 재귀 호출을 통한 DFS 구현  
        for(int i = 0; i < N; i++){  
            if(!visit[i]){  
                visit[i] = true;  
                foo[depth] = i+1;  
                DFS(depth+1);  
                visit[i] = false;  
            }  
        }  
  
    }  
  
    public static void printArray(int[] array){  
  
        for(int i = 0; i < array.length ; i++){  
            System.out.print(array[i] + " ");  
        }  
        System.out.println();  
    }  
  
}

Leave a comment