Problem Solving/BOJ

[BOJ] 15664 : N과 M (10)

shyram 2018. 12. 30. 02:39
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
41
42
43
#include <bits/stdc++.h>
using namespace std;
 
int n, m;
int arr[8];
int ans[8];
bool visited[8];
void solve(int depth, int index) {
    if (depth == m) {
        for (int i = 0; i < m; i++cout << ans[i] << ' ';
        cout << '\n';
        return;
    }
    
    vector<int> same_search;
 
    for (int i = index; i <= n - (m - depth); i++) {
        bool check = true;
        for (int j = 0; j < same_search.size(); j++) {
            if (same_search[j] == arr[i]) {
                check = false;
                break;
            }
        }
        
        if (check && !visited[i]) {
            same_search.push_back(arr[i]);
            ans[depth] = arr[i];
            visited[i] = true;
            solve(depth + 1, i + 1);
            visited[i] = false;
        }
    }
}
 
int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> m;
    for (int i = 0; i < n; i++cin >> arr[i];
    sort(arr, arr + n);
    solve(00);
}
cs