求助,样例过了,但是33分

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

yaoxuelin @ 2023-06-15 14:49:04

#include<bits/stdc++.h>
using namespace std;
int visited[11],n,k;
void bfs(int rst,string a){
    if(rst == 0){cout << a << endl;return;}
    for(int i = 1;i <= n;i++){
        if(visited[i]) continue;
        visited[i] = 1;
        bfs(rst - 1,a + to_string(i) + " ");
        visited[i] = 0;
    }
}
int main(){
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    for(int i = 1;i <= n;i++){
        visited[i] = 1;
        bfs(k - 1,to_string(i) + " ");
        visited[i] = 0;
    }
    return 0;
}

by linchengjun @ 2023-07-01 13:53:17


using namespace std;
int n,m,a[10010];
bool b[10010];
void dfs(int k){
    if(k>m){
        for(int i=1;i<=m;i++) cout << a[i] << " ";
        cout << endl;
        return;
    }
    for(int i=1;i<=n;i++){
        if(!b[i]){
            a[k]=i;
            b[i]=1;
            dfs(k+1);
            b[i]=0; 
        }
    }
}
int main(){
    cin >> n >> m;
    dfs(1);
    return 0;
}

by 280123xxx @ 2023-07-31 00:11:15

#include <iostream>
using namespace std;
const int N=20;
bool st[N];//数字状态 
int state[N];//坑位状态 
int n,k;
void dfs(int u)
{
  if(u>k)
  {
    for(int i=1;i<=k;i++)
    {
        cout<<state[i]<<' ';
    }
    puts("");
    return ;
  }

  for(int i=1;i<=n;i++)
  {
    if(!st[i])
    {
    st[i]=true;
    state[u]=i;
    dfs(u+1);
    state[u]=0;
    st[i]=false;
   }
  }
}
int main()
{
    cin>>n>>k;
    dfs(1);
    return 0;
} 

|