dfs模板

dfs模板

b站视频链接

b站视频链接

#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 20;
int arr[maxn];
int vis[maxn];
int stc[maxn];
int n,top;
void init(){
    for(int i = 0;i < maxn;++i){
        vis[i] = 1;
    }
}
void dfs(int ith){
    if(ith == n){
        for(int i = 0;i<n;++i){
            cout<<stc[i];
        }
        cout<<endl;
        return ;
    }
    for(int i = 0;i<n;++i){
        if(vis[i] == 1){
            vis[i] = 0;
            stc[top++] = arr[i];
            dfs(ith+1);
            top--;
            vis[i] = 1;
        }
    }
}

int main()
{
    while(cin>>n){
        for(int i = 0;i<n;++i){
            cin>>arr[i];
        }
        init();
        top = 0;
        dfs(0);
    }
    return 0;
}
Last modification:June 25th, 2020 at 09:03 pm
如果觉得我的文章对你有用,请随意赞赏

2 comments

  1. OP

    老张 ლ(′◉❥◉`ლ)

    1. 子不语
      @OP

      你是哪个0.0

Leave a Comment