public static void solve(int idx){
if(list.size()==m){
for (Integer i:list) System.out.print(i+" ");
System.out.println();
list.remove(list.size()-1);
}
for(int i=idx;i<=n;i++){
if (!list.contains(i)){
list.add(i);
solve(i+1);
}
}
}
public static void solve(int idx){
//1이추가된다
list.add(idx);
if(list.size()==m){
for (Integer i:list) System.out.print(i+" ");
System.out.println();
}
for(int i=idx;i<=n;i++){
if (!list.contains(i)){
solve(i+1);
list.remove(list.size()-1);
}
}
}
public static void solve(int idx){
if(list.size()==m){
for (Integer i:list) System.out.print(i+" ");
System.out.println();
return;
}
for(int i=idx;i<=n;i++){
if (!list.contains(i)){
list.add(i);
solve(i);
list.remove(list.size()-1);
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.OptionalInt;
public class Main{
static int n;
static int m;
static ArrayList<Integer> list;
public static void solve(){
if(list.size()==m){
for (Integer i:list) System.out.print(i+" ");
System.out.println();
return;
}
for(int i=1;i<=n;i++){
if (!list.contains(i)){
list.add(i);
solve();
list.remove(list.size()-1);
}
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String input[] = br.readLine().split(" ");
n = Integer.parseInt(input[0]);
m = Integer.parseInt(input[1]);
list = new ArrayList<>();
solve();
bw.flush();
bw.close();
}
}
https://www.acmicpc.net/problem/5568
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;
public class Main{
static int n;
static int k;
static String arr[];
static ArrayList<String>list = new ArrayList<>();
static boolean visited[];
public static void dfs(int idx, String num, int count){
if(idx > n) return;
if(count ==k){
if(!list.contains(num)) list.add(num);
return;
}
for(int i=1;i<arr.length;i++){
if(!visited[i]){
visited[i] = true;
dfs(idx+1, num + arr[i], count+1);
dfs(idx+1, num, count);
visited[i] = false;
}
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
n = Integer.parseInt(br.readLine());
k = Integer.parseInt(br.readLine());
visited = new boolean[n+1];
arr = new String[n+1];
for(int i=1;i<=n;i++){
arr[i] = br.readLine();
}
dfs(0,"",0);
bw.write(list.size()+"");
bw.flush();
bw.close();
}
}
dfs(idx+1, num + arr[i], count+1);
dfs(idx+1, num, count);//이줄이 없어도된다
왜냐하면 반복문을 통해 그다음 반복에선 arr[i]
을 미포함시키고 진행할것이니깐.