uva 331 Mapping the Swaps 求交换排序的map 纯DFS
给出一个序列,每次交换两个数,求有几种交换方法能使序列变成升序。
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的更多相关文章
- UVA Mapping the Swaps
题目例如以下: Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entrie ...
- uva331 - Mapping the Swaps
Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the ...
- 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。
这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...
- uva 315 Network(无向图求割点)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 1453 - Squares 旋转卡壳求凸包直径
旋转卡壳求凸包直径. 参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <cstdio> ...
- UVA 796 - Critical Links (求桥)
Critical Links In a computer network a link L, which interconnects two servers, is considered criti ...
- UVA 315 315 - Network(求割点个数)
Network A Telephone Line Company (TLC) is establishing a new telephone cable network. They are con ...
- poj 3565 uva 1411 Ants KM算法求最小权
由于涉及到实数,一定,一定不能直接等于,一定,一定加一个误差<0.00001,坑死了…… 有两种事物,不难想到用二分图.这里涉及到一个有趣的问题,这个二分图的完美匹配的最小权值和就是答案.为啥呢 ...
- UVA 796 Critical Links(无向图求桥)
题目大意:给你一个网络要求这里面的桥. 输入数据: n 个点 点的编号 (与这个点相连的点的个数m) 依次是m个点的 输入到文件结束. 桥输出的时候需要排序 知识汇总: 桥: 无向连通 ...
随机推荐
- 高能天气——团队Scrum冲刺阶段-Day 1-领航
高能天气--团队Scrum冲刺阶段-Day 1-领航 各个成员在 Alpha 阶段认领的任务 经过重新的团队讨论,我们最新确定的α版本所需实现内容如下: 查找城市:切换城市按钮.滑动界面视图 天气预报 ...
- 017.Zabbix宏介绍
一 宏介绍 Zabbix宏的作用是便于在模板.Items.Trigger中的引用,名称为{$名称},宏的字符范围为:A~Z/0~9/_. 如:net.tcp.service[ssh,{$SSH_POR ...
- centos yum 安装php mysql
1 安装php7 查看 centos 版本 # cat /etc/centos-release 删除之前的 php 版本 # yum remove php* php-common rpm 安装 Php ...
- IEnumerable<T>
IEnumerable 饮水思源 <C#本质论> Overview 根据定义,.Net 的中集合,本质上是一个类,它最起码实现了IEnumeraable 或者非泛型的IEnumerable ...
- Java 操纵XML之创建XML文件
Java 操纵XML之创建XML文件 一.JAVA DOM PARSER DOM interfaces The DOM defines several Java interfaces. Here ar ...
- php7 & lua 压测对比
内存:32G CPU:2个6核 接口数据deflate 压缩后 均不到10k, ==== php7 ==== Concurrency Level: 100 Time taken for tests: ...
- hdu 2710 水题
题意:判断一些数里有最大因子的数 水题,省赛即将临近,高效的代码风格需要养成,为了简化代码,以后可能会更多的使用宏定义,但是通常也只是快速拿下第一道水题,涨自信.大部分的代码还是普通的形式,实际上能简 ...
- 微服务架构的分布式事务解决方案 - zhaorui2017的博客 - CSDN博客
微服务架构的分布式事务解决方案 - zhaorui2017的博客 - CSDN博客 http://blog.csdn.net/zhaorui2017/article/details/7643679 ...
- .net下的span和memory
.net core 2.1的重头戏就是性能,其中最重要的两个类就是span和memory,本文这里简单的介绍一下这两个类的使用. 什么是 Span<T> Span<T> 是新一 ...
- poi excel导入导出
pom <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artif ...