You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
따라서 기존에 백트래킹 하기 전에 push_back했던 숫자를 i로 둬도 상관이 없습니다. 다음으로 push할 숫자도 마찬가지로 꼭 start일 필요가 없습니다. 재귀의 시작지점이 아니라 항상 1이어도 상관없게 됩니다. 왜냐하면 순서를 고려하지 않기 때문입니다.
최종 코드
#include<bits/stdc++.h>usingnamespacestd;int N, M;
vector<int> combination;
voiddfs() {
if (combination.size() == M) {
for (int num: combination) {
cout << num << "";
}
cout << "\n";
return;
}
for (int i = 1; i <= N; i++) {
combination.push_back(i);
dfs(); // 다음 숫자
combination.pop_back();
}
}
intmain() {
cin >> N >> M;
dfs();
return0;
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
문제: https://www.acmicpc.net/problem/15651
선행문제: 15650번 N과 M(2)
앞서 올렸던 문제 다음 단계의 문제였습니다. 코드 살짝만 바꾸면 바로 풀리는 문제였는데 두 부분만 신경쓰면 됐어요.
따라서 기존에 백트래킹 하기 전에 push_back했던 숫자를 i로 둬도 상관이 없습니다. 다음으로 push할 숫자도 마찬가지로 꼭 start일 필요가 없습니다. 재귀의 시작지점이 아니라 항상 1이어도 상관없게 됩니다. 왜냐하면 순서를 고려하지 않기 때문입니다.
최종 코드
Beta Was this translation helpful? Give feedback.
All reactions