[C++][정올][Intermediate_Coder] 1681 : 해밀턴 순환회로

[C++][정올][Intermediate_Coder] 1681 : 해밀턴 순환회로

코드

#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
int N, answer = 10000000;
int board[12][12] = {0};
 
 
void dfs(int idx, int ans, vector<int> &check){
    if (find(check.begin(), check.end(), 0)==check.end()){
        if (board[idx][0]>0){
            answer = min(answer, ans+board[idx][0]);
        }
        return;
    }
     
    for (int i=0; i<N; i++){
        if(check[i]==0 and board[idx][i]>0){
            check[i]=1;
            dfs(i, ans+board[idx][i], check);
            check[i]=0;
        }
    }
}
 
int main() {
    cin>>N;
    for (int x=0; x<N; x++){
        for (int y=0; y<N; y++){
            cin>>board[x][y];
        }
    }
    vector<int> check(N);
    check[0]=1;
    dfs(0,0,check);
     
    cout<<answer;
}

Discussion and feedback