uva 331 Mapping the Swaps 求交换排序的map 纯DFS
来源:互联网 发布:网络摄像头密码重置 编辑:程序博客网 时间:2024/04/28 22:39
给出一个序列,每次交换两个数,求有几种交换方法能使序列变成升序。
n不大于5,用dfs做。
代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int num[8], ans, n;bool check() {//check if the array is inorderfor (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++;elsedfs();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 331 Mapping the Swaps (回溯+dfs)
- UVa 331 Mapping the Swaps (DFS)
- uva 331 Mapping the Swaps(DFS+回溯)
- UVA Mapping the Swaps (DFS)
- uva 331 Mapping the Swaps
- uva 331 - Mapping the Swaps
- UVa 331 - Mapping the Swaps
- uva 331 - Mapping the Swaps
- UVa 331 - Mapping the Swaps
- UVA 331 - Mapping the Swaps
- UVa 331 - Mapping the Swaps
- UVA 331 Mapping the Swaps
- uva 331 Mapping the Swaps
- uva 331 Mapping the Swaps
- uva 331 Mapping the Swaps
- UVa:331 Mapping the Swaps
- UVa 331 - Mapping the Swaps
- 字符串16进制存储
- ZOJ 1076 最长上升子序列
- HDU 4473 Exam 2012成都现场赛J题(规律+思维)
- shǎ崽 OrOrOrOrz
- mac 字符串转换为二进制
- uva 331 Mapping the Swaps 求交换排序的map 纯DFS
- ZOJ 1710 The Snail
- log4j日志等级
- linux小记
- LA 3644
- ZOJ 1093 最大高度
- Tomcat6和Tomcat7配置SSL通信的比较
- CentOS6 vsFTP安装与配置
- 利用 Xdebug+WinCacheGrind 分析PHP程序,找出性能瓶颈