UVA 331 Mapping The Swaps (backtracking)

Source: Internet
Author: User

UVA 331 Mapping The Swaps


Sorting an array can be do by swapping certain pairs of adjacent entries in the array. This was the fundamental technique used in the well-known bubble sort. If We list the identities of the pairs to being swapped, in the sequence they is to is swapped, we obtain what might is call Ed a swap map. For example, suppose we wish to sort the array A whose elements is 3, 2, and 1 in that order. If the subscripts for this array is 1, 2, and 3, sorting the array can be accomplished by swapping A2 and A3, then Swappi Ng A1 and A2, and finally swapping A2 and A3. If a pair is identified in a swap map by indicating the subscript of the first element of the pair to being swapped, then thi s sorting process would is characterized with the swap map 2 1 2.

It is instructive to note this there may be many ways in which swapping of adjacent array entries can be used to sort an A Rray. The previous array, containing 3 2 1, could also be sorted by swapping A1 and A2, then swapping A2 and A3, and finally SWA Pping A1 and A2 again. The swap map that describes this sorting sequence is 1 2 1.

For a given array, how many different swap maps exist? A little thought would show that there is an infinite number of swap maps, since sequential swapping of A arbitrary pair Of elements won't change the order of the elements. Thus the Swap map 1 1 1 2 1 would also leave our arrays elements in ascending order. But how many swap maps of minimum size would place a given array in order? That is, the question you and answer in this problem.

Input

The input data would contain an arbitrary number of test cases, followed by a single 0. Each test case would have a integersn that gives the size of an array, and would be followed by the n integ Er values in the array.

Output

For each test case, print a message similar through those shown in the sample output below. In no test case wouldN be larger than 5.

Sample Input
2 9 72 12 503 3 2 13 9 1 50
Sample Output
There is 1 swap maps for input data set 1.There is 0 swap maps for input data set 2.There is 2 swap maps for input data Set 3.There is 1 swap maps for input data set 4.


Main topic: given a sequence of numbers, and then you have to exchange the two adjacent number of methods, with a minimum number of exchanges to sort out how many kinds of scenarios

problem-solving idea: The method of bubble sort is the scheme with the least number of exchanges.



#include <stdio.h> #include <string.h>int num[10], min, n;int check () {for (int i = 1; i < n; i++) {if (Num[i] < num[i-1]) return 0;} return 1;} void swap (int &a, int &b) {int temp;temp = A;a = B;b = temp;} void DFS () {if (check ()) {Min++;return;} for (int i = 0; i < n-1; i++) {if (Num[i] > num[i + 1]) {swap (num[i],num[i + 1]);D FS (); Swap (Num[i], num[i + 1]);}  }}int Main () {int case = 1;while (scanf ("%d", &n) = = 1, n) {memset (num, 0, sizeof (NUM)); for (int i = 0; i < n; i++) {scanf ("%d", &num[i]);} min = 0;if (!check ()) {DFS ();} printf ("There is%d swap maps for input data set%d.\n", Min, case++);} return 0;}



UVA 331 Mapping The Swaps (backtracking)

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.