N and M (1)

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

language

목록 보기
3/24

I did solve most of questions at SSAFY though. After entering company, I forgot everything. So Remind.

  1. Making All path. Backtracking.
  2. Print
import java.util.*;
import java.io.*;

public class Main {

  private static boolean[] visited;
  private static int[] arr;

  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, N, M);

  }

  private static void dfs(int step, int N, int M) {
    if (step == M) {
      for (int i = 0; i < M; i++) {
        System.out.print(arr[i] + " ");
      }
      System.out.println("");
      return;
    }

    for (int i = 0; i < N; i++) {
      if (visited[i] == false) {
        visited[i] = true;

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

        visited[i] = false;
      }
    }

    return;
  }
}
profile
Are you nervous? Don't be

0개의 댓글