给出一个序列,每次交换两个数,求有几种交换方法能使序列变成升序。

n不大于5,用dfs做。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int num[8], ans, n; bool check() { //check if the array is inorder
for (int i = 0; i < n - 1; i++)
if (num[i] > num[i + 1])
return false;
return true;
} void dfs(void) {
for (int i = 0; i < n - 1; i++)
if (num[i] > num[i + 1]) {
swap(num[i], num[i + 1]);
if (check())
ans++;
else
dfs();
swap(num[i], num[i + 1]);
}
} int main () {
int cas = 0;
while (scanf("%d", &n) && n) {
for (int i = 0; i < n; i++)
scanf("%d", &num[i]);
ans = 0;
if (!check()) dfs();
printf("There are %d swap maps for input data set %d.\n", ans, ++cas);
}
return 0;
}

uva 331 Mapping the Swaps 求交换排序的map 纯DFS的更多相关文章

  1. UVA Mapping the Swaps

    题目例如以下: Mapping the Swaps  Sorting an array can be done by swapping certain pairs of adjacent entrie ...

  2. uva331 - Mapping the Swaps

    Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the ...

  3. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

  4. uva 315 Network(无向图求割点)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. UVa 1453 - Squares 旋转卡壳求凸包直径

    旋转卡壳求凸包直径. 参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <cstdio> ...

  6. UVA 796 - Critical Links (求桥)

    Critical Links  In a computer network a link L, which interconnects two servers, is considered criti ...

  7. UVA 315 315 - Network(求割点个数)

     Network  A Telephone Line Company (TLC) is establishing a new telephone cable network. They are con ...

  8. poj 3565 uva 1411 Ants KM算法求最小权

    由于涉及到实数,一定,一定不能直接等于,一定,一定加一个误差<0.00001,坑死了…… 有两种事物,不难想到用二分图.这里涉及到一个有趣的问题,这个二分图的完美匹配的最小权值和就是答案.为啥呢 ...

  9. UVA 796 Critical Links(无向图求桥)

    题目大意:给你一个网络要求这里面的桥. 输入数据: n 个点 点的编号  (与这个点相连的点的个数m)  依次是m个点的   输入到文件结束. 桥输出的时候需要排序   知识汇总: 桥:   无向连通 ...

随机推荐

  1. maven deploy上传私服出错

    error 内容如下 Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.5:  deploy (default ...

  2. [转]C++ template —— 模板基础(一)

    <C++ Template>对Template各个方面进行了较为深度详细的解析,故而本系列博客按书本的各章顺序编排,并只作为简单的读书笔记,详细讲解请购买原版书籍(绝对物超所值).---- ...

  3. [CQOI2009]跳舞

    思路:二分答案+最大流.二分答案$m$,表示最多跳$m$轮.将每个人拆成两个点$a_i$$b_i$,$a_i$表示与任何人跳舞,$b_i$表示与不喜欢的人跳舞.对于第$i$个人,连一条从$a_i$到$ ...

  4. BZOJ4205 : 卡牌配对

    对于两张卡牌,如果存在两种属性值不互质,则可以匹配. 只考虑200以内的质数,一共有46个,可以新建3*46*46个点来表示一类属性值中有这两种质数的卡牌. 然后对于每张卡牌,枚举它的质因子,最多只有 ...

  5. 【BZOJ】3732: Network【Kruskal重构树】

    3732: Network Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2812  Solved: 1363[Submit][Status][Dis ...

  6. SNMP代理软件开发

    SNMP代理模块包括6个子模块: SNMP协议主要有五种报文get.get-next.set.get-response,trap.l.get-request操作:从代理进程处提取一个或多个参数值2.g ...

  7. linux_远程copy

    1:远程copy [linux对linux 远程拷贝]   scp 文件名 root@远程ip:/路径/   将本地home目录下的test.tar的文件拷贝到远程主机192.168.1.23的/ho ...

  8. perl解析xml-XML::Simple/XMLin

    转自: http://blog.charlee.li/perl-xml-simple/ [Perl]用XML::Simple解析XML文件 在Perl中解析XML的方法最常见的就是使用 XML::DO ...

  9. Android论坛

    APKBUS:http://www.apkbus.com/forum.php 看雪ANDROID:http://bbs.pediy.com http://www.52pojie.cn http://w ...

  10. Unity3d学习笔记记录

    1.发布到 ipad字体显示不出来,改变Position位置的Z轴为-1 2.发布打包有问题,记得用户权限有没有设置 3.ipad4分辨率:2048*1536 4.调整界面大小,尽量调整底下子对象位置 ...