码迷,mamicode.com
首页 > 移动开发 > 详细

uva 331 Mapping the Swaps (回溯)

时间:2015-02-02 18:07:12      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:uva   c语言   

                               uva 331 Mapping the Swaps


Sorting an array can be done by swapping certain pairs of adjacent entries in the array. This is the fundamental technique used in the well-known bubble sort. If we list the identities of the pairs to be swapped, in the sequence they are to be swapped, we obtain what might be called a swap map. For example, suppose we wish to sort the array A whose elements are 3, 2, and 1 in that order. If the subscripts for this array are 1, 2, and 3, sorting the array can be accomplished by swapping A2 and A3, then swapping 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 be swapped, then this sorting process would be characterized with the swap map 2 1 2.

It is instructive to note that there may be many ways in which swapping of adjacent array entries can be used to sort an array. The previous array, containing 3 2 1, could also be sorted by swapping A1 and A2, then swapping A2 and A3, and finally swapping 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 will show that there are an infinite number of swap maps, since sequential swapping of an arbitrary pair of elements will not change the order of the elements. Thus the swap map 1 1 1 2 1 will also leave our array elements in ascending order. But how many swap maps of minimum size will place a given array in order? That is the question you are to answer in this problem.

Input

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

Output

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

Sample Input

2 9 7
2 12 50
3 3 2 1
3 9 1 5
0

Sample Output

There are 1 swap maps for input data set 1.
There are 0 swap maps for input data set 2.
There are 2 swap maps for input data set 3.
There are 1 swap maps for input data set 4.


题目大意:给定一个数字序列,然后要你用交换相邻两个数的方法, 用最少交换次数进行排序的方案数有多少种

解题思路:冒泡排序的方法是交换次数最小的方案。



#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]);
			DFS();
			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 are %d swap maps for input data set %d.\n", min, Case++);
	}
	return 0;
}



uva 331 Mapping the Swaps (回溯)

标签:uva   c语言   

原文地址:http://blog.csdn.net/llx523113241/article/details/43411347

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!