-
[Dev-Ping9] 백준 15649번 - N과 M (3) (Java) [DFS]코딩테스트/백준 2021. 12. 18. 17:24
# 문제설명
DFS 문제이다. N과 M (1)번 문제와 다르게 숫자를 중복으로 고를 수 있다.
노드의 방문처리유무를 표시하지 않아주면 된다.
# 코드 구현
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main{ static boolean[] picked; static int[] space; static StringBuilder sb = new StringBuilder(); static void dfs(int idx, int nodeEnd,int limit){ if(idx == limit){ // printArr(space); for(int num:space){ sb.append(num).append(" "); } sb.append("\n"); return; } for(int i=1; i<=nodeEnd;i++){ if(!picked[i]){ // picked[i]=true; space[idx]=i; dfs(idx+1,nodeEnd,limit); // picked[i]=false; } } } public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()," "); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); picked = new boolean[n+1]; space = new int[m]; dfs(0,n,m); System.out.print(sb); } }
728x90'코딩테스트 > 백준' 카테고리의 다른 글
[Dev-Ping9] 백준 9663번 - N-Queen (Java) [DFS] (0) 2021.12.19 [Dev-Ping9] 백준 15649번 - N과 M (4) (Java) [DFS] (0) 2021.12.18 [Dev-Ping9] 백준 15649번 - N과 M (2) (Java) [DFS] (0) 2021.12.18 [Dev-Ping9] 백준 15649번 - N과 M (1) (Java)[DFS] (0) 2021.12.18 [Dev-Ping9] 백준 1436번 - 영화감독 숌 (Java) (0) 2021.12.18