https://www.acmicpc.net/problem/15649
15649번: N과 M (1)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해
www.acmicpc.net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| // Global Variable | |
| int N, M; | |
| bool visited[9]; | |
| int saved_arr[9]; | |
| // Definition | |
| void Input(); | |
| void Dfs(int cnt); | |
| // Main | |
| int main() { | |
| Input(); | |
| Dfs(0); | |
| } | |
| // Declaration | |
| void Input() { | |
| freopen("input.txt", "r", stdin); | |
| scanf("%d %d", &N, &M); | |
| } | |
| void Dfs(int cnt) { | |
| if (cnt == M) { | |
| for (int i = 0; i < M; i++) { | |
| printf("%d ", saved_arr[i]); | |
| } | |
| printf("\n"); | |
| return; | |
| } | |
| for (int i = 1; i <= N; i++) { | |
| if (visited[i] == true) continue; | |
| visited[i] = true; | |
| saved_arr[cnt] = i; | |
| Dfs(cnt + 1); | |
| visited[i] = false; | |
| } | |
| } |
'C++ > CodingTest' 카테고리의 다른 글
| [백준 1080] 행렬.cpp (0) | 2022.08.28 |
|---|---|
| [백준 2606] 바이러스.cpp (0) | 2022.08.25 |
| [백준 1766 - 골드 2] 문제집.cpp (0) | 2022.05.06 |
| [백준 1520 - 골드 4] 내리막 길.cpp (0) | 2022.05.06 |
| [백준 1620 - 실버 4] 나는야 포켓몬 마스터 이다솜.cpp (0) | 2022.05.06 |