Notice
Recent Posts
Recent Comments
Link
Little Jay
[C++] 백준 15651번 - N과 M (3) 본문
이번에는 중복순열 문제이다
이전에는 중복을 체크하면서 DFS를 했는데,
이번에는 중복을 허용하기때문에 그 부분만 주석처리 해주어 쉽게 풀 수 있었다.
#include <iostream>
#include <cstring>
#define MAX 9
using namespace std;
int n, m;
int arr[MAX] = { 0, };
bool visited[MAX] = { 0, };
void dfs(int cnt) {
if (cnt == m) {
for (int i = 0; i < m; i++)
cout << arr[i] << ' ';
cout << '\n';
return;
}
for (int i = 1; i <= n; i++) {
//if (!visited[i])
{
visited[i] = true;
arr[cnt] = i;
dfs(cnt + 1);
visited[i] = false;
}
}
}
int main() {
cin >> n >> m;
dfs(0);
return 0;
}'알고리즘 > BOJ' 카테고리의 다른 글
| [C++] 백준 17298번 - 오큰수 (0) | 2021.11.08 |
|---|---|
| [C++] 백준 15652번 - N과 M (4) (0) | 2021.11.06 |
| [C++] 백준 15650번 - N과 M(2) (0) | 2021.11.06 |
| [C++] 백준 7875번 - Brackets (0) | 2021.09.26 |
| [C++] 백준 1712번 - 손익분기점 (0) | 2021.09.25 |
Comments