nyoj19擅长排列的小明

xiaoxiao2021-02-28  22

题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=19

擅长排列的小明

时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算。比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他,在这5个数字中选出几个数字让他继续全排列,那么你就错了,他同样的很擅长。现在需要你写一个程序来验证擅长排列的小明到底对不对。 输入 第一行输入整数N(1<N<10)表示多少组测试数据, 每组测试数据第一行两个整数 n m (1<n<9,0<m<=n) 输出 在1-n中选取m个字符进行全排列,按字典序全部输出,每种排列占一行,每组数据间不需分界。如样例 样例输入 2 3 1 4 2 样例输出 1 2 3 12 13 14 21 23 24 31 32 34 41 42 43

虽然题目归类在stl,但是我是用深搜写的。

#include<bits/stdc++.h> using namespace std; int a[15]; int n,s; void show() { int t=0; while(t<s) { t++; for(int i=1;i<=n;i++) { if(a[i]==t) { cout<<i; break; } } } cout<<endl; } void dfs(int x,int y) { //cout<<"dfs:"<<x<<','<<y<<endl; a[x]=y; for(int i=1;i<=n&&y+1<=s;i++) { if(a[i]) continue; else { dfs(i,y+1); } } if(y==s) { show(); } a[x]=0; } int main() { int t; cin>>t; while(t--) { cin>>n>>s; memset(a,0,sizeof(a)); for(int i=1;i<=n;i++) { dfs(i,1); memset(a,0,sizeof(a)); } } }

转载请注明原文地址: https://www.6miu.com/read-2602645.html

最新回复(0)