๐ ๋ฌธ์ ๋งํฌ
๐ ํ์ด ๊ณผ์
- ์ฌ๊ท ์ ํ์ ๋ฌธ์ ์
๋๋ค.
- 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 boolean[] visited;
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());
visited = new boolean[n + 1];
arr = new int[m];
solve(0);
}
public static void solve(int idx){
if(idx == m){
for(int i = 0; i< m; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
return;
}
for(int i = 1; i<= n; i++){
if(visited[i] && arr[idx - 1] == i){
visited[i] = true;
arr[idx] = i;
solve(idx + 1);
visited[i] = false;
}
else if(!visited[i]){
if(idx == 0 || arr[idx - 1] < i){
visited[i] = true;
arr[idx] = i;
solve(idx + 1);
visited[i] = false;
}
}
}
}
}
'Algorithm > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค 15657๋ฒ : N๊ณผ M (8) (Java) (0) | 2022.01.17 |
---|---|
๋ฐฑ์ค 15654๋ฒ : N๊ณผ M (5) (Java) (0) | 2022.01.17 |
๋ฐฑ์ค 11659๋ฒ : ๊ตฌ๊ฐ ํฉ ๊ตฌํ๊ธฐ 4(Java) (0) | 2022.01.17 |
๋ฐฑ์ค 17626๋ฒ : Four Squares(Java) (0) | 2022.01.17 |
๋ฐฑ์ค 7662๋ฒ : ์ด์ค ์ฐ์ ์์ ํ(Java) (0) | 2022.01.10 |