全wa!样例过了,但是全错

B3623 枚举排列(递归实现排列型枚举)

Camelliail @ 2024-03-03 15:24:09

#include<bits/stdc++.h>
using namespace std;

/*B3623 枚举排列*/
int n,r, pd[100], used[100];
void dfs(int m,int k) {
    if (k == 0) {
        for (int j = 1; j <= r; j++) {
            cout << used[j] << " ";
        }
        cout << endl;
        return;
    }
    for (int i = 1; i <= n; i++) {
        if(!pd[i]){//如果该数没被用过
            pd[i] = 1;
            used[m + 1] = i;//m从0层开始
            dfs(m + 1, k - 1);
            pd[i] = 0;//回溯
        }   
    }
}
int main() {
    cin >> n >> r;
    dfs(0,2);
    return 0;
}

by hexuchen @ 2024-03-03 15:26:29

@Camelliail

#include<bits/stdc++.h>
using namespace std;

/*B3623 枚举排列*/
int n,r, pd[100], used[100];
void dfs(int m,int k) {
    if (k == 0) {
        for (int j = 1; j <= r; j++) {
            cout << used[j] << " ";
        }
        cout << endl;
        return;
    }
    for (int i = 1; i <= n; i++) {
        if(!pd[i]){//如果该数没被用过
            pd[i] = 1;
            used[m + 1] = i;//m从0层开始
            dfs(m + 1, k - 1);
            pd[i] = 0;//回溯
        }   
    }
}
int main() {
    cin >> n >> r;
    dfs(0,r); //参数改成r
    return 0;
}

by Camelliail @ 2024-03-03 16:13:41

@hexuchen 谢谢你!!


|