ps연습장
백준 15649번 N과 M (1) 풀이 (C/C++)
hwsyl
2023. 9. 15. 22:01
반응형
<문제 : 백준 15649번 >
https://www.acmicpc.net/problem/15649
15649번: N과 M (1)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해
www.acmicpc.net
<풀이>
백트래킹을 이용하면 간단하게 해결할 수 있다.
<구현>
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
45
46
47
48
49
50
51
|
#include<bits/stdc++.h>
#define fio() \
ios_base::sync_with_stdio(0); \
cin.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef tuple<int, int, int> tpi;
typedef tuple<ll, ll, ll> tpl;
typedef pair<double, ll> pdl;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };
const double pi = acos(-1);
const int MOD = 1000000007;
typedef tuple<double, int, int> dii;
bool visited[9];
int n, M, ans[9];
void f(int m){
if(m >= M){
for(int i = 0; i < M; i++){
printf("%d ", ans[i]);
}
printf("\n");
return;
}
for(int i = 1; i <= n; i++){
if(visited[i]) continue;
ans[m] = i;
visited[i] = true;
f(m+1);
visited[i] = false;
}
return;
}
int main(){
scanf("%d %d", &n, &M);
f(0);
}
|
cs |