Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1027
Recently, the school broke the net, VJ did not go, ran out of the Internet.
Do not want to write the problem, write a little bit simple not to let oneself rusty.
The number of N to find the full array of M, this question how can be, but a violent DFS need to be aware of the point is to add a flag to determine whether the current match is output the full array.
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <algorithm>5#include <iostream>6#include <cmath>7#include <queue>8#include <map>9#include <Set>Ten#include <stack> One#include <list> A#include <vector> - - using namespacestd; the - Const intMAXN =1010; - intN, m, flag; - intVIS[MAXN]; + intARR[MAXN]; - + voidSolveintk) { A if(flag) { at return ; - } - if(k = =N) { -m--; - if(!m) { - for(inti =0; I < n; i++) { inprintf"%d", Arr[i]); -Putchar (i < n-1?' ':'\ n'); toFlag =1; + } - } the } * for(inti =1; I <= N; i++) { $ if(!Vis[i]) {Panax NotoginsengVis[i] =1; -ARR[K] =i; theSolve (k +1); +Vis[i] =0; A } the } + } - $ intMain () { $ //freopen ("in", "R", stdin); - while(~SCANF ("%d%d", &n, &m)) { -memset (Vis,0,sizeof(Vis)); theFlag =0; -Solve0);Wuyi } the}
View Code
STL Solution:
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <algorithm>5#include <iostream>6#include <cmath>7#include <queue>8#include <map>9#include <Set>Ten#include <stack> One#include <list> A#include <vector> - - using namespacestd; the - intN, M; - intarr[1010]; - + intMain () { - //freopen ("In.txt", "R", stdin); + while(~SCANF ("%d%d", &n, &m)) { A for(inti =1; I <= N; i++) { atArr[i] =i; - } - while(--m) { -Next_permutation (arr+1, arr+n+1); - } - for(inti =1; I <= N; i++) { inprintf"%d", Arr[i]); -Putchar (i < n?)' ':'\ n'); to } + } -}
View Code
[HDOJ1027] Ignatius and the Princess II