UVA - 331 Mapping the Swaps
来源:互联网 发布:航迹规划 算法 编辑:程序博客网 时间:2024/06/16 04:20
题目大意:给一个整数数列排序,只能交换相邻数字,问交换次数最少的方法有几种。
解题思路:因为只允许交换相邻数字,冒泡就是交换次数最少的方法,保证不要进行无意义的交换,然后每完成一次排序次数 +1 即可。
#include<iostream> #include<cstdio>#include<cmath>#include<string>#include<stdlib.h>#include<algorithm>using namespace std;int num[10];int n, ans, tmp, cnt = 0;bool check() { for (int i = 0; i < n-1; i++) if (num[i] > num[i+1]) return false; return true;}void dfs() { if (check()) { ans++; return; } for (int i = 0; i < n-1; i++) if (num[i] > num[i+1]) { swap(num[i], num[i+1]); dfs(); swap(num[i], num[i+1]); }}int main() { while (scanf("%d", &n) != EOF && 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, ++cnt); }return 0; }
0 0
- 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
- UVa 331Mapping the Swaps
- uva 331 - Mapping the Swaps
- UVa 331 - Mapping the Swaps
- UVA 331 - Mapping the Swaps
- UVA - 331 Mapping the Swaps
- 用python写个死锁
- UVA - 301 Transportation
- 美团推荐算法实战
- UVA - 10344 23 out of 5
- java基础总结
- UVA - 331 Mapping the Swaps
- iOS-GCD中dispatch source使用
- 剑指Offer_06 根据前序遍历和中序遍历序列 重建二叉树
- UIImage和UIImageView的区别
- https和http区别
- 代理模式(java版)
- 流程模块开发步骤
- HDU 3555 Bomb [数位DP]【动态规划】
- 关于aidl文件的启用