Xiaoming, who is good at arranging in nyoj19 -- dfs

Xiaoming, who is good at arranging

Time limit: 1000 ms | memory limit: 65535 KB

Difficulty: 4

  • describe

    Xiao Ming is very clever and very good at arranging and calculating. For example, if you give Xiao Ming a number of 5, he can immediately give a full range of 1-5 according to the dictionary order. If you want to embarrass him, select a few numbers from the five numbers to let him continue the full range, then you are wrong. He is also good at it. Now you need to write a program to verify that Xiao Ming is good at arranging.

  • input

    Enter the integer N (1) in the first line

2
3 1
4 2
  • sample output
1
2
3
12
13
14
21
23
24
31
32
34
41
42
43
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int book[11];
void dfs(string &ans,const int n,const int m,int num){
    if(num==m){
        cout<<ans<<endl;
        return;
    }
    for(int i=1;i<=n;i++){
        if(!book[i]){
            book[i]=1;
            ans+=(i+'0');
            dfs(ans,n,m,num+1);
            book[i]=0;
            ans.erase(num);
        }
    }
}
int main(){
    //freopen("/Users/zhaohaibo/Desktop/test.txt","r",stdin);
    int num;
    cin>>num;
    while(num--){
        int n,m;//n is the number between 1 and n, m is the number to be used
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            string ans;
            memset(book,0,sizeof(book));
            book[i]=1;
            ans+=(i+'0');
            dfs(ans,n,m,1);
        }
    }
}
#include <string>
#include <string.h>
#include <iostream>
#define maxn 11
using namespace std;
int book[maxn];
char data[maxn];
void dfs(const int max_num,const int total,int now_num){
    if(now_num==total){
        data[total]='\0';
        cout<<data<<endl;
        return;
    }
    for(int i=1;i<=max_num;i++){
        if(!book[i]){
            book[i]=1;
            data[now_num] = i+'0';
            dfs(max_num,total,now_num+1);
            book[i]=0;
            return;
        }
    }
}
int main(){
    freopen("/Users/zhaohaibo/Desktop/test.txt","r",stdin);
    int N;
    cin>>N;
    while(N--){
        memset(book,0,sizeof(book));
        int max_num,total;
        cin>>max_num>>total;
        dfs(max_num,total,0);
    }
}

Added by CSmith1128 on Mon, 23 Mar 2020 17:09:24 +0200