uva 331 - Mapping the Swaps

点击打开链接

题目意思:给定一个无序的序列,要求找到一个交换次数最少的方案使得序列为有序列,然后再找到以该最少次数为方案的总数是多少,输出方案数

解题思路:根据冒泡排序的方法,我们将可以得到最少的交换次数,然后问我们在这个次数下的交换方式有几种。我们可以利用冒泡排序的思想,就是我们每一次递归下去的时候都从0这个点开始搜索,只要有递归就判断当前的序列是否排好序,如果序列排好那么ans++并且不再递归下去直接return,注意这里每一次递归回来还要把两个数交换回来

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <algorithm>
using namespace std;
const int MAXN = 1000;

int n, ans;
int arr[5];

//判断函数

int judge() {
    for (int i = 0; i < n - 1; i++) {
        if (arr[i] > arr[i + 1])
            return 0;
    }
    return 1;
}

//递归函数

void dfs() {
    if (judge()) {
        ++ans;//序列已经排好
        return;
    }
    for (int k = 0; k < n - 1; k++) {//这里每一次都从0开始搜索
        if (arr[k] > arr[k + 1]) {
            swap(arr[k], arr[k + 1]);
            dfs();
            swap(arr[k], arr[k + 1]);//状态的回溯
        }
    }
}

int main() {
    int cnt = 1;
    while (scanf("%d", &n) && n) {
        ans = 0;
        for (int i = 0; i < n; i++)
            scanf("%d", &arr[i]);
        if (!judge())
            dfs();
        printf("There are %d swap maps for input data set %d.\n", ans, cnt);
        ++cnt;
    }
    return 0;
}
时间: 2024-11-13 06:42:50

uva 331 - Mapping the Swaps的相关文章

UVa 331 Mapping the Swaps (DFS)

331 - Mapping the Swaps Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=267 Sorting an array can be done by swapping certain pairs of adjacent entries in

UVa 331:Mapping the Swaps

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=267 题目类型: 回溯 原题: Sorting an array can be done by swapping certain pairs of adjacent entries in the array. This is the fundame

UVa 10716: Evil Straw Warts Live

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1657 [原题] A palindrome is a string of symbols that is equal to itself when reversed. Given an input string, not necessarily a

UVa 112:Tree Summing 二叉树构造, 二叉树路径和

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=48&mosmsg=Submission+received+with+ID+10280379 题目类型: 数据结构, 二叉树 加强版样例输入: 22 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()())))

UVa 755 / POJ 1002 487--3279 (排序)

755 - 487--3279 Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=696 http://poj.org/problem?id=1002 Businesses like to have memorable telephone numbers. On

UVa 299 Train Swapping:冒泡排序的次数

299 - Train Swapping Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=235 At an old railway station, you may still encounter one of the last remaining ``tr

UVa 10168 Summation of Four Primes:“1+1+1+1”问题

10168 - Summation of Four Primes Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1109 思路:既然1+1都在很大的数据范围内成立,那我们不妨先分出两个素数来,然后把剩下的数分成两个素数. 完整代码: 01./*0.025s*

使用Flash实现Bump Mapping的效果

1.  下面先来简单的介绍一下什么叫作Bump Mapping.Bump Mapping又叫做凹凸贴图,在现在的图形处理以及游戏当中非常流行(大家应该都玩过Halflife-2或者Doom吧?).凹凸贴图与普通贴图最大的区别是,凹凸贴图中的每个象素不仅代表着点的颜色,还代表着这个点凹凸的程度. 所以凹凸贴图(Bump Mapping)也经常被用来渲染一些看上去比较复杂的材质,比如凹凸不平的路面.生锈的铁罐.人类的皮肤还有剥落的墙面等等.下面是一张凹凸贴图的效果图, 它是由一张Bump Map(凹

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.