개발자 김수진

15650 - N과M(2) 본문

알고리즘/백준

15650 - N과M(2)

김수진장 2020. 5. 11. 09:06

N과 M(1)에서 DFS의 매개변수로 index를 추가해주어 오름차순 조건을 맞춰줬다.

처음에 main에서 DFS 호출할 때 for문 사용해서 결과가 제대로 안나왔는데

진짜 DFS 이해를 아직 제대로 못한 것 같다. 

 

#include <iostream>

using namespace std;

int N,M;
int visited[8];


void DFS(int cnt, int idx)
{
    if(cnt == M)
    {
        for(int i=0;i<N;i++){
            if(visited[i])
                cout << i+1 << " ";
        }
        cout << "\n";
        
    }
    
    for(int i=idx;i<N;i++){
        if(!visited[i]){
            visited[i]=1;
            DFS(cnt+1,i+1);
            visited[i]=0;
        }
    }
}

int main(void)
{
    cin >> N >> M ;

    DFS(0,0);
    return 0;
    
}

'알고리즘 > 백준' 카테고리의 다른 글

15652-N과M(4)  (0) 2020.05.11
15651 - N과M(3)  (0) 2020.05.11
15649 - N과 M(1)  (0) 2020.05.11
14891(톱니바퀴)  (0) 2020.04.30
백준 - 2667(단지번호붙이기)  (0) 2020.04.26