2 条题解

  • 0
    @ 2026-10-17 9:10:56

    #include<bits/stdc++.h> using namespace std; int n,m; bool used[20]; int path[20]; void dfs(int pos){ if(pos==m){ for(int i = 0;i<m;i++){ printf("%3d",path[i]); } printf("\n"); return ; } for(int i =1;i<=n;i++){ if(!used[i]){ used[i] = true; path[pos] = i; dfs(pos+1); used[i] = false; } } } int main(){ cin>>n>>m; cout<<n<<" "<<m<<endl; dfs(0); return 0; }

    • 0
      @ 2026-10-17 9:00:29

      #include<bits/stdc++.h> using namespace std; int a[10001],b[10001],n,m; void dfs(int k){

      for(int i = 1;i<=n;i++){
      	if(b[i]==0){
      		a[k]=i;
      		b[i]=1;
      		if(k==m){
      			for(int i = 1;i<=m;i++){
      				cout<<setw(3)<<a[i];
      			}
      			cout<<endl;
      		}else{
      			dfs(k+1);
      		}
      		b[i]= 0;
      	}
      }
      

      } int main(){ cin>>n>>m; cout<<n<<" "<<m<<endl; dfs(1); return 0; }

      • 1

      信息

      ID
      z20261006
      时间
      1000ms
      内存
      32MiB
      标签
      递交数
      21
      已通过
      5
      上传者