boj)15650 - N과 M (2)

2020. 11. 16. 11:22PS/boj

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.*;
import java.util.Scanner;
 
public class boj_15650 {
    static Scanner sc = new Scanner(System.in);
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static int[] numbers = new int[10];
    static boolean[] visited = new boolean[10];
    static int n, m;
 
    public static void main(String[] args) throws IOException {
        n = sc.nextInt();
        m = sc.nextInt();
 
        func(0);
        bw.flush();
        bw.close();
    }
 
    static void func(int index) throws IOException {
        if (index == m) {
            for (int i = 0; i < m; i++) {
                bw.append(numbers[i] + " ");
            }
            bw.newLine();
            return;
        }
 
        for (int i = 1; i <= n; i++) {
            if (visited[i]) continue;
            if (index != 0 && numbers[index-1> i) continue;
 
            numbers[index] = i;
            visited[i] = true;
            func(index + 1);
            visited[i] = false;
        }
    }
}
 
cs

 

- 백트래킹

 

- 뇌가 뒤틀리는거 같다.

- 중간에 조건 추가해야하는건 알겠는데 어떤 부분에 어떤식으로 넣어야할지 느낌이 잘 안온다.

 

 

 

 


www.acmicpc.net/problem/15650

'PS > boj' 카테고리의 다른 글

boj)5525 - IOIOI  (0) 2020.11.17
boj)15651 - N과 M (3)  (0) 2020.11.16
boj)15649 - N과 M (1)  (0) 2020.11.16
boj)5904 - Moo 게임  (0) 2020.11.15
boj)2447 - 별 찍기 - 10  (0) 2020.11.14