less than 1 minute read

  • 분류: 백트래킹

코드

#include <iostream>

int n, m;
int seq[9];

void dfs(int depth) {
    if(depth==m) {
        for(int a=0 ; a<m ; a++) {
            std::cout << seq[a] << ' ';
        }
        std::cout << "\n";
        return;
    }
    for(int i=0 ; i<n ; i++) {
        seq[depth] = i+1;
        dfs(depth+1);
    }
}

int main() {
    std::cin >> n >> m;
    dfs(0);
}
  • 코멘트

    앞 두 문제 백준 15649번백준 15650번의 쉬운 버전이다. (왜 이게 3인지..) 방문 여부를 따지지 않는 단순한 dfs로 해결할 수 있다. dfs라 부르기도 민망한 수준이다.