HDU - 2181 Hamilton bypass the world's problems (bfs)

Meaning of the questions: given 20 cities adjacent three points, given the starting point, (after all other cities and then return to that area) then all output paths (and lexicographically minimum)
idea: to find the path with dfs , then the corresponding lexicographically smallest on the first row are connected to each city urban primary sequence

 

Complete code:

#include <the iostream> 
#include <cstdio> 
#include <algorithm> 
#include <CString>
 the using  namespace STD;
 int G [ 25 ] [ . 5 ];
 int TOT; // record the number of paths and reaches the i-th point 
int ANS [ 25 ];
 int VIS [ 25 ];
 int m; // starting point 
void DFS ( int S, int CNT) {
     IF (CNT> . 19 ) return ; // termination 
     //从s开始的步骤为cnt结束
    for(int i=0;i<3;i++){
        if(cnt==19&&g[s][i]==m){
            cout<<++tot<<": ";
            for(int i=0;i<=19;i++){
                cout<<" "<<ans[i];
            }
            cout<<" "<<m<<endl;
            return ;
        }else IF (! VIS [G [S] [I]] = . 1 ) { // the next position not accessed 
            ANS [CNT + . 1 ] =   G [S] [I]; 
            VIS [G [S] [I]] = . 1 ; 
            DFS (G [S] [I], CNT + . 1 ); 
            VIS [G [S] [I]] = 0 ; // back 
        } 
    } 
    
} 
int main () {
     int S [ . 3 ];
     // enter 
    for ( int I = . 1 ; I <= 20 is ; I ++ ) { 
        CIN >> S [0]>>s[1]>>s[2];
        sort(s,s+3);
        g[i][0] = s[0];
        g[i][1] = s[1];
        g[i][2] = s[2];
    }
    int S;
    
    while(cin>>S){
        if(!S) break;
        tot = 0;
        memset(vis,0,sizeof(vis));
        m = S,vis[m] = 1;// After the first recording determination vis 
        ANS [ 0 ] = m; 
        DFS (S, 0 ); 
    } 
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/Tianwell/p/11265608.html