N and M (2)

문학적인유사성·2022년 2월 20일
0

language

목록 보기
4/24
  1. No Duplicate
  2. Ascending
    Just same as sequence

I reckon that everything about CodingTest Stuff is back. :)

It is just basic stuff.....though...

import java.io.*;
import java.util.*;

public class Main {
  private static int[] arr;
  private static boolean[] visited;

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String[] str = br.readLine().split(" ");

    int N = Integer.parseInt(str[0]);
    int M = Integer.parseInt(str[1]);

    visited = new boolean[N];
    arr = new int[M];

    dfs(0, 0, N, M);

  }

  private static void dfs(int step, int x, int N, int M) {
    if (step == M) {

      for (int i = 0; i < M; i++) {
        System.out.print(arr[i] + " ");
      }
      System.out.println("");
      return;
    }
 	//have to change to against from  N and M (1)
    for (int i = x; i < N; i++) {
      if (visited[i] == false) {
        visited[i] = true;

        arr[step] = i + 1;
        dfs(step + 1, i + 1, N, M);

        visited[i] = false;
      }
    }
    return;

  }
}
profile
Are you nervous? Don't be

0개의 댓글