-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15654.cpp
More file actions
44 lines (38 loc) · 802 Bytes
/
15654.cpp
File metadata and controls
44 lines (38 loc) · 802 Bytes
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
44
#include <iostream>
#include <vector>
#include <algorithm>
#define io ios::sync_with_stdio(false); cin.tie(0);
using namespace std;
int N,M;
int nums[9];
int res[9];
bool promising(int n,int depth){
for(int i=0;i<depth;i++){
if(res[i]==n) return false;
}
return true;
}
void backtracking(int n,int depth){
if(promising(n,depth)){
if(depth==M-1){
for(int i=0;i<M;i++){
cout<<res[i]<<' ';
}
cout<<'\n';
}else{
for(int i=0;i<N;i++){
res[depth+1]=nums[i];
backtracking(nums[i],depth+1);
}
}
}
}
int main()
{
io;
cin>>N>>M;
for(int i=0;i<N;i++) cin>>nums[i];
sort(&nums[0],&nums[0]+N);
backtracking(0,-1);
return 0;
}