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

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. MySQL主从检验一致性工具pt-table-checksum报错的案例分析

    [问题] 有同事反馈我们改造过的MySQL5.7.23版本,使用pt-table-checksum工具比较主从数据库的一致性时报错 Unsafe statement written to the bi ...

  2. Xamarin iOS教程之警告视图

    Xamarin iOS教程之警告视图 Xamarin iOS警告视图 如果需要向用户显示一条非常重要的消息时,警告视图(UIAlertView类)就可以派上用场了.它的功能是把需要注意的信息显示给用户 ...

  3. 修改无线wifi网络名称。注册表。windows 无线属性 windows 无线 配置文件

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha windows 无线属性 windows 无线 配置文件 ======= 修改完成,之后 ...

  4. 14、Redis的复制

    写在前面的话:读书破万卷,编码如有神 --------------------------------------------------------------------------------- ...

  5. Springboot_StringRedisTemplate配置

    @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { Str ...

  6. 使用 IntraWeb (17) - 基本控件之 TIWRadioButton、TIWRadioGroup、TIWCheckBox

    TIWRadioButton //单选 TIWRadioGroup //单选组 TIWCheckBox //复选 TIWRadioButton 所在单元及继承链: IWCompRadioButton. ...

  7. Oracle数据库备份还原工具之Expdp/IMPdp

    使用EXPDP和IMPDP时应该注意的事项: EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用, ...

  8. ftp通用类2

    using System; using System.Net; using System.IO; using System.Text; using System.Net.Sockets; /// &l ...

  9. HappyJTAG2 - JTAG AND SPI AVR8 interface EMBEDDED JTAG ! EMBEDDED SPI !

    New version released ! V2.45 (Check version list for details) This construction is based on HappyJTA ...

  10. WICED SDK 3.3.1

    7/20/2015 UPDATE: After installing the IDE you may not see all the APPs.  Press F5 in Eclipse to ref ...