[HDOJ1027] Ignatius and the Princess II

Source: Internet
Author: User
Tags cmath

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.