๋ฐ์ํ
https://www.acmicpc.net/problem/15652
15652๋ฒ: N๊ณผ M (4)
ํ ์ค์ ํ๋์ฉ ๋ฌธ์ ์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์์ด์ ์ถ๋ ฅํ๋ค. ์ค๋ณต๋๋ ์์ด์ ์ฌ๋ฌ ๋ฒ ์ถ๋ ฅํ๋ฉด ์๋๋ฉฐ, ๊ฐ ์์ด์ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํด์ ์ถ๋ ฅํด์ผ ํ๋ค. ์์ด์ ์ฌ์ ์์ผ๋ก ์ฆ๊ฐํ๋ ์์๋ก ์ถ๋ ฅํด์ผ ํ๋ค.
www.acmicpc.net
์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static int arr[];
public static int N;
public static int M;
public static StringBuilder sb = new StringBuilder();
public static void dfs(int count) {
if(count == M) {
for(int i=0; i<M-1; i++) {
if(arr[i] > arr[i+1])
return;
}
for(int i : arr)
sb.append(i + " ");
sb.append("\n");
return;
}
for(int i=1; i<=N; i++) {
arr[count] = i;
dfs(count+1);
}
}
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str = bf.readLine();
StringTokenizer st = new StringTokenizer(str);
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
arr = new int[M];
dfs(0);
System.out.println(sb);
bf.close();
}
}
ํ์ด
N๊ณผ M(2) + N๊ณผ M(3) ๋ ๋ฌธ์ ๋ฅผ ์์ด ๋ง๋ ๋ฌธ์ ์ด๋ค.
์ค๋ณตํ์ฉ + ๋น๋ด๋ฆผ์ฐจ์
์ค๋ณตํ์ฉ -> ๋ฐฉ๋ฌธ์ฒดํฌ X, ๊ฐ์ ๊ณณ ํ์ํ ์ ์๋ค.
๋น๋ด๋ฆผ์ฐจ์ -> arr[i] > arr[i+1] ์ด ์ฐธ์ผ๋, return; ์ ํตํด ์ฌ๊ทํธ์ถํ dfs()๋ฉ์๋๋ฅผ ์ข ๋ฃํ๋ค.
๋ฐ์ํ
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Codeforces] 1097A: Gennady and a Card Game(brute force) (0) | 2020.02.21 |
---|---|
[๋ฐฑ์ค] 1932๋ฒ: ์ ์ ์ผ๊ฐํ(DP, ๋์ ๊ณํ๋ฒ) (0) | 2020.02.20 |
[Codeforces] 996A - Hit the Lottery (0) | 2020.02.20 |
[๋ฐฑ์ค] 15651๋ฒ: N๊ณผ M (3) (dfs, ์ค๋ณตํฌํจ) (0) | 2020.02.19 |
[๋ฐฑ์ค] 2217๋ฒ: ๋กํ(๊ทธ๋ฆฌ๋, ์ํ) (0) | 2020.02.19 |
๋๊ธ