Enfoque DFS totalmente organizado

#include <iostream> 
#include <cstring>
 usando el  espacio de nombres std;
const  int maxn = 1e3;
int vis [maxn];
int p [maxn];
int n;
int t = 0 ;
nulo dfs ( int x) 
{ 
    if (x == n + 1 ) 
    { 
        for ( int i = 1 ; i <= n; i ++ ) 
            cout << p [i] << "  " ; 
            cout << endl;
            regreso ;
    } 
    para ( int i = 1 ; i <= n; i ++ ) 
    { 
        if (! vis [i]) 
        { 
            vis [i] = 1 ; 
            p [x] = i; 
            dfs (x + 1 ); 
            vis [i] = 0 ; 
        } 
    } 
} 
int main () 
{ 
    while (cin >> n) 
    { 
        memset (vis, 0 , sizeof (vis)); 
        dfs ( 1 ); 
    }
}

 

Supongo que te gusta

Origin www.cnblogs.com/liyexin/p/12680850.html
Recomendado
Clasificación