Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 백준
- 위상정렬
- 시뮬레이션
- CS
- 구현
- 스택
- 유니온 파인드
- 완전탐색
- dfs
- mst
- Effective Java
- 프로그래머스
- swea
- 백트래킹
- 후니의 쉽게 쓴 시스코 네트워킹
- JUnit 5
- 플로이드-와샬
- 동적계획법
- java
- BFS
- 알고리즘
- 투 포인터
- Network
- 그리디
- 에라토스테네스의 체
- 수학
- 세그먼트 트리
- Kotlin
- 이분탐색
- 문자열
Archives
반갑습니다!
[백준] 15654 N과 M (5) 본문
풀이
백트래킹을 사용해서 순열을 구해주면 되는데, 이 때 숫자 배열은 정렬된 상태여야한다.
코드
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int n, m;
int num[8];
bool chk[8];
vector<int> ans;
void dfs(int idx, int cnt) {
if (cnt == m) {
for (int i : ans)
cout << i << ' ';
cout << '\n';
return;
}
for (int i = 0; i < n; i++) {
if (!chk[i]) {
chk[i] = true;
ans.push_back(num[i]);
dfs(i, cnt + 1);
ans.pop_back();
chk[i] = false;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> num[i];
sort(num, num + n);
dfs(-1, 0);
return 0;
}
'알고리즘 문제 풀이' 카테고리의 다른 글
[백준] 17780 새로운 게임 (0) | 2020.04.19 |
---|---|
[백준] 3568 iSharp (0) | 2020.04.19 |
[SWEA] 최장 경로 (0) | 2020.04.19 |
[SWEA] 2817 부분 수열의 합 (0) | 2020.04.18 |
[SWEA] 1213 String (0) | 2020.04.18 |