데이터 엔지니어링 정복/Algorithm
[브루트포스, 백트래킹] 백준 - N과 M (2)
eeaarrtthh
2021. 8. 31. 19:50
728x90
반응형
https://www.acmicpc.net/problem/15650
-자바
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int n, m;
static int[] arr;
static StringBuilder sb = new StringBuilder();
public static void dfs( int index, int depth ) {
if( depth == m ) {
for( int val : arr ) sb.append( val ).append( " " );
sb.append( "\n" );
return;
}
for( int i=index; i<=n; i++ ) {
arr[depth] = i;
dfs( i+1, depth+1 );
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer( br.readLine() );
n = Integer.parseInt( st.nextToken() );
m = Integer.parseInt( st.nextToken() );
arr = new int[m];
dfs( 1, 0 );
System.out.println( sb );
}
}
-파이썬
from sys import stdin
def dfs( index, depth ):
global n, m, arr, s
if depth == m:
for val in arr: s += str(val)+" "
s += "\n"
return
for i in range( index, n+1 ):
arr[depth] = i
dfs( i+1, depth+1 )
input = stdin.readline
n, m = map(int, input().split())
arr = [0]*m
s = ""
dfs( 1, 0 )
print( s )
728x90
반응형