[백준] 15650번 N과 M (2) / C++
#문제
#풀이
#include <iostream>
#define MAX 9
using namespace std;
int n, m;
int arr[MAX] = { 0 };
int visited[MAX] = { 0 };
void dfs(int num, int cnt)
{
if (cnt == m)
{
for (int i = 0; i < m; ++i)
{
cout << arr[i] << " ";
}
cout << '\n';
}
for (int i = num; i <= n; ++i)
{
if (!visited[i])
{
visited[i] = true;
arr[cnt] = i;
dfs(i + 1, cnt + 1);
visited[i] = false;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
dfs(1, 0);
return 0;
}
#정리
n개의 배열을 m개의 열로 중복 없이 출력하는 문제. 중복된 수 조합이 출려되면 안 되므로 dfs를 사용하여 해결.
Enjoy Reading This Article?
Here are some more articles you might like to read next: