Algorithm/Baekjoon
๋ฐฑ์ค 15649 N๊ณผ M(1)
do-oni
2021. 10. 30. 18:45
๐ปQ
15649๋ฒ: N๊ณผ M (1)
ํ ์ค์ ํ๋์ฉ ๋ฌธ์ ์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์์ด์ ์ถ๋ ฅํ๋ค. ์ค๋ณต๋๋ ์์ด์ ์ฌ๋ฌ ๋ฒ ์ถ๋ ฅํ๋ฉด ์๋๋ฉฐ, ๊ฐ ์์ด์ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํด์ ์ถ๋ ฅํด์ผ ํ๋ค. ์์ด์ ์ฌ์ ์์ผ๋ก ์ฆ๊ฐํ๋ ์์๋ก ์ถ๋ ฅํด
www.acmicpc.net
๐กA
import java.io.*; import java.util.StringTokenizer; public class Main { static int n, m; static int[] arr; static boolean[] visited; static StringBuilder sb = new StringBuilder(); 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]; visited = new boolean[n]; dfs(0); System.out.println(sb); } public static void dfs(int depth) { if(depth==m) { for(int n : arr) { sb.append(n).append(' '); } sb.append('\n'); return; } for(int i=0; i<n; i++) { if(!visited[i]) { visited[i] = true; arr[depth] = i + 1; dfs(depth + 1); visited[i] = false; } } } }โ