๋ฐ์ํ
https://www.acmicpc.net/problem/15651
์ฝ๋
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 : 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 ์๋ฆฌ์ฆ 3๋ฒ์งธ๋ฌธ์ .
์๋ฅผ ๋ฝ๋ ๊ฐ์ ์๋ฅผ ์ฌ๋ฌ ๋ฒ ๊ณจ๋ผ๋ ๋๋ค. - ์ค๋ณต ํ์ฉํ๋ ๋ฌธ์ ๋ค.
๋ฐ๋ผ์ ์ด์ ์ ๋ฌธ์ ๋ 1์ ํ์ํ๊ณ , ๋ฐฉ๋ฌธํ์๊ฒฝ์ฐ 2๋ฅผํ์ํ๋ ์์ผ๋ก dfs๋ฅผ ๋๋ ธ์ผ๋,
์ด ๋ฌธ์ ๋ 1์ ํ์ํ๊ณ ๋ค์ 1์ ํ์ํ๋ค.(count๊ฐ ์ฃผ์ด์ง M๊ณผ ๊ฐ์๋๊น์ง) - ์์ ํ์.
* ์ด ๋ฌธ์ ์์ Scanner์ ๊ธฐ๋ณธ์ ์ธ ์ถ๋ ฅ์ ํ ๊ฒฝ์ฐ ์๊ฐ์ด๊ณผ๊ฐ ๋ฐ์ํ๋๋ฐ,
StringBuilder + BufferedReader ํด๋์ค๋ฅผ ํ์ฉํด ์๊ฐ ์ด๊ณผ๊ฐ ๋ฐ์ํ์ง ์์๋ค.
๋ฐ์ํ
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 15652๋ฒ: N๊ณผ M (4) (dfs, ์ค๋ณตํฌํจ, ๋น๋ด๋ฆผ์ฐจ์) (0) | 2020.02.20 |
---|---|
[Codeforces] 996A - Hit the Lottery (0) | 2020.02.20 |
[๋ฐฑ์ค] 2217๋ฒ: ๋กํ(๊ทธ๋ฆฌ๋, ์ํ) (0) | 2020.02.19 |
[Codeforces] 1154A - Restoring Three Numbers (0) | 2020.02.19 |
[๋ฐฑ์ค] 15650๋ฒ: N๊ณผ M (2) (dfs, ๋ฐฑํธ๋ํน) (0) | 2020.02.18 |
๋๊ธ