Nyist OJ 19 is good at arranging James (DFS search + STL)

Source: Internet
Author: User
Good at arranging James time limit: 1000 MS | memory limit: 65535 kb difficulty: 4
Description
James is very clever and very good at arranging computing. For example, if you want to give a small number 5, he can immediately give a full array of numbers from 1 to 5 in the lexicographically ordered order. If you want to embarrass him, select a few numbers from these five numbers to keep him in full order, then you are wrong. He is also very good at it. Now you need to write a program to verify whether James is good at sorting.
Input
The number of groups of test data in the first line is represented by an integer N (1 <n <10,
The first line of each group of test data is two integers n m (1 <n <9, 0 <m <= N)
Output
In 1-N, select M characters for full sorting, and output all data in lexicographically. Each sort occupies one row, and there is no division between each group of data. Example
Sample Input
23 14 2
Sample output
123121314212324313234414243
Source
[Hzyqazasdf] original
Uploaded

Hzyqazasdf

This question has been done before; it is done using the next_permutation function in STL. I can't remember this question again today, I have learned something before, but I have to review it for a while. Today I have done it again with DFS search + backtracking recursion. The key is to train my thinking skills;

The next_permutation function is used to output all the columns larger than the current one. The order is from small to large. In contrast, there is also a prev_permutation function;

The prev_permutation () function outputs all the smaller orders than the current one. The order is from large to small.

If you are not familiar with it, you can compile a test function to test it;

# Include <iostream> # include <cstring> # include <algorithm> using namespace STD; int main () {// int A [] = {3, 1, 2 }; int A [] = {1, 2, 3 }; do {cout <A [0] <"" <A [1] <"" <A [2] <Endl;} while (next_permutation (, A + 3); // A + 3 is the size of the array // while (prev_permutation (A, A + 3); Return 0 ;}
You can test the results of the two groups of data;

The first group of {3, 1, 2} uses next to get the results of {3, 1, 2} and {3, 2, 1 };

The result obtained by Pre is {, 2}, {, 1}, {, 3}, {, 2,}, {, 3 };

In the second group {, 3}, the result obtained with next is {, 3}, {, 2}, {, 3}, {, 1, 2 },{ 3, 2, 1 };
The result obtained by Pre is {1, 2, 3 };

We can see from the above conclusion: to get a full arrangement, we need to sort the given array; the next function defaults to the order from small to large, the default order of the PRE function is from large to small;

For details about the principles of these two functions, refer to these two blogs;

Http://leonard1853.iteye.com/blog/1450085 http://www.cnblogs.com/mycapple/archive/2012/08/13/2635853.html

Let's take a look at the code for the above question:

# Include <cstdio> # include <cstring> # include <algorithm> char a [10] = {'1', '2', '3', '4 ', '5', '6', '7', '8', '9', '\ 0'}; // Array Using namespace STD with a given size sequence number; int main () {int t, n, m; char B [10], C [10]; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); strcpy (B, A); // The full sorting starts in the lexicographically ascending order, therefore, the first group of data is copied directly from small to large. B [m] = '\ 0'; // copy the previous m printf ("% s \ n", B ); while (next_permutation (A, A + n) // arrange the array in full {strcpy (C, A); C [m] = '\ 0 '; if (strcmp (B, c) // judge whether arrays B and C are equal {strcpy (B, c ); // output B [m] = '\ 0'; printf ("% s \ n", B) ;}} return 0 ;}

The following is written recursively. The main idea is search + backtracking;

Judge from 1 to see if the number is already in the sequence, and then judge the next one;

Understand the concept of recursion;

The following code is used:

# Include <cstdio> # include <cstring> int visit [20], a [20]; // mark the array int n, m; void DFS (int pos) {If (Pos = m) // indicates the end of recursion {for (INT I = 0; I <m; I ++) printf ("% d ", A [I]); // outputs printf ("\ n"); return;} For (INT I = 1; I <= N; I ++) {If (! Visit [I]) // This is not accessed {visit [I] = 1; // it indicates that a [POS] = I has been accessed; // The first number of this sequence DFS (Pos + 1); // search for the next number visit [I] = 0; // backtracking }}int main () {int T; scanf ("% d", & T); While (t --) {memset (visit, 0, sizeof (visit )); // initialize scanf ("% d", & N, & M); DFS (0);} return 0 ;}


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.