uva 331 Mapping the Swaps (回溯)

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;
}
时间: 2024-08-27 11:15:03

uva 331 Mapping the Swaps (回溯)的相关文章

UVA Mapping the Swaps

题目例如以下: Mapping the Swaps  Sorting an array can be done by swapping certain pairs of adjacent entriesin the array. This is the fundamental technique used in the well-knownbubble sort. If we list the identities of the pairs to be swapped, in thesequen

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 t

UVa 331 交换的方案数 (回溯法,启发)

题意:只能交换相邻的数.要把一个数组这样交换以形成升序.问最少交换次数的不同交换顺序有多少种. 思路:还是没想到,看到别人题解中一句话,茅塞顿开:每次从头选两个需要交换的位置进行交换.只有降序才需要交换,而且可以看到每次降序的调换都是有意义的.这样每次从头找一个需要交换的位置,就是不同的方案.当某次从头扫描到尾没有需要交换的位置时,则已经排好序,就是交换次数,用一个flag变量标志. 0.022s,还好,就没有剪枝优化.这题还是可以剪枝的. 这题对回溯法的应用和思路很有启发意义~ Code: #

uva 11218 KTV(DFS+回溯)

uva 11218 KTV One song is extremely popular recently, so you and your friends decided to sing it in KTV. The song has 3 characters, so exactly 3 people should sing together each time (yes, there are 3 microphones in the room). There are exactly 9 peo

UVa 129 Krypton Factor【回溯】

学习的紫书的回溯,理解起来还是好困难的说啊= = 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #includ

uva 193 Graph Coloring(回溯)

uva 193 Graph Coloring You are to write a program that tries to find an optimal coloring for a given graph. Colors are applied to the nodes of the graph and the only available colors are black and white. The coloring of the graph is called optimal if

UVa 129 Krypton Factor (DFS &amp;&amp; 回溯)

题意 : 如果一个字符串包含两个相邻的重复子串,则称它是"容易的串",其他串称为"困难的 串".例如,BB.ABCDACABCAB.ABCDABCD都是容易的串,而D.DC.ABDAB. CBABCBA都是困难的串.程序从输入中读取多行数据,每行包括两个整数n和L(即按此顺序给出),其中n > 0,L的范围是1 ≤ L ≤ 26.根据这些输入,程序要按照字母表升序打印出第n个"hard"字串(由字母表中的前L个字母构成),并在接下来的一行打

UVa 1602 网格动物(回溯)

https://vjudge.net/problem/UVA-1602 题意:计算n连通块不同形态的个数. 思路: 实在是不知道该怎么做好,感觉判重实在是太麻烦了. 判重就是判断所有格子位置是否都相同,这样我们可以定义一个结构体来保存每个格子的坐标点,用set容器poly来保存这些格子,然后再用一个set容器poly_set来保存指定数量i个连通块的各个图形的坐标点,也就是说该容器是用来保存poly的.(不太好解释,具体可以看代码.)因为图形必须是连通的,所以在添加第i个格子的时候必定是在i-1

UVA 331 交换的方案数

题意:交换一个数组的相邻两个元素可以达到对数组排序的功能,类似于冒泡排序,但交换的方案可能不止一种.比如说数组A[3]为3,2,1,要想排为1,2,3,可以先交换位置1和2的元素(数组变为2,3,1),然后交换位置2和3的元素(变为2,1,3),最后交换位置1和2的(变为1,2,3),此为方案一,具体可以用1,2,1(数字即每次交换的第一个数的位置)来描述.当然还可以先交换位置2和3(312),然后交换位置1和2(132)最后交换位置2和3(123),如上的描述方法便是2,1,2,此为方案二.当