15649 N과 M (1) (JAVA)

Fekim·2022년 2월 24일
0

ps

목록 보기
15/48
  • 결과를 ArrayList에 담았다가 빼면서 출력을 하니 시간초과가 났다... 결과에 담아 출력했더니 다시 통과가 되더라
  • 노드를 탐색하고 나올때, 체크배열을 다시 false로 바꿔줘야 함을 유의하자.
public class Main {

    static int n,m;
    static boolean[] ch;
    static int[] res;
    public static void dfs(int L){
        if(L == m){
            for(int i:res)
                if(i != 0)
                    System.out.print(i+" ");
            System.out.println();
            return ;
        }
        else{
            for(int i=1; i<=n; ++i){
                if(!ch[i]){
                    ch[i] = true;
                    res[L+1] = i;
                    dfs(L+1);
                    ch[i] = false;
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        ch = new boolean[n+1];
        res = new int[m+1];
        dfs(0);
    }
}
profile
★Bugless 2024★

0개의 댓글