算法模板

wiaxiamm / 2023-08-11 / 原文

【DFS】
class Solution {
public:
    int n;
    int path[10000];
    bool st[10000];
    void dfs(int u) {
        if(u==n){
            for(int i=0;i<n;++i)cout<<path[i]<<" ";
            cout<<endl;
            return;
        }
        for(int i=1;i<=n;++i){
            if(!st[i]){
                path[u]=i;
                st[i]=1;
                dfs(u+1);
                st[i]=0;
            }
        }
    }
    
};

int main(){
    auto c=Solution();
    c.n=4;
    c.dfs(0);
    return 0;
}