
算法入门经典第二版第七章暴力求解法
文章平均质量分 50
MrFox_
1.少说话, 多做事。
2.现在就是将来, 本来现实, 何必幻想!
展开
-
UVA - 140 Bandwidth
//枚举排列的一个应用吧#include#include#include#includeusing namespace std;const int maxn = 26;int vis[maxn][maxn];int res[maxn];int ans[maxn];int k[maxn];int main(){ string kk; while(getl原创 2015-07-26 14:05:01 · 450 阅读 · 0 评论 -
UVA - 1343 The Rotation Game
//这个题我并没有AC 但是也花了很长时间了 保存一下代码 也经常来看看。下面链接是ac代码点击打开链接//然后是我的代码, 其实我TlE了之后各种修改, 最后看看跟上面的AC代码几乎一模一样了, 但是有一个地方不同,就是move 这个地方, 但我自己算的时间复杂度没有什么差距呀, 不过在电脑上运行确实有差距, 我的很慢, 先贴上吧, 以后懂了会再修改。。。//#inclu原创 2015-07-28 20:17:47 · 399 阅读 · 0 评论 -
UVA - 208 Firetruck
//非常水的一道回溯题, 只是输出问题醉了, 题目里的输出不是AC的输出格式;#include#include#include#include#includeusing namespace std;const int maxn=20 + 5;int res[maxn][maxn];int par[maxn];int r[maxn];int used[maxn];vecto原创 2015-08-11 21:19:49 · 359 阅读 · 0 评论 -
UVA - 1374 Power Calcul
//今天上午优化了一上午的UVA1374, 下午看这个题, 最后也是看的网上的代码。。。。想自己一个一个的做,但是训练计划太紧了。很无奈的感觉。感觉这个题还是需要比较强的思维逻辑能力啊。自己做应该是不行了。。#include#include#include#includeusing namespace std;const int maxn = 2000;int n, res[ma原创 2015-07-28 19:55:21 · 453 阅读 · 0 评论 -
UVA - 11212 Editing a Book
//迭代加深法, 可以解决一些看起来更适合用BFS和回溯法的题,但是BFS和回溯法浪费空间,所以迭代加深法更能解决问题。。。#include#include#include#includeusing namespace std;int n, h;int maxd;int solve(int *tmp, int d){ h = 0; for(int i = 0原创 2015-07-27 10:01:35 · 463 阅读 · 0 评论 -
UVA - 12325 Zombie's Treasure Chest
//并没有感觉到奇妙之处,可能是因为书上讲的太细了,感觉就是个经验题。。#include#include#include#includeusing namespace std;#define MAX 100000int main(){ int T; cin >> T; int kase = 0; while(T--) {原创 2015-07-27 10:53:34 · 627 阅读 · 0 评论 -
UVA - 129 Krypton Factor
//回溯法中避免不必要的判断,就像八皇后问题中,只需判断新皇后和之前的皇后是否冲突,而不必要判断和以前的皇后冲突。#include#include#include#includeusing namespace std;const int maxn = 80 + 5;int S[maxn];int n, L;int cnt;int dfs(int cur){ if(c原创 2015-07-26 10:27:01 · 447 阅读 · 0 评论 -
八皇后问题
//刘汝佳的书用八皇后问题引出了回溯法。看思路分析:首先暴力枚举子集, 2的64次方,显然不行;其次是从64个格子中选8个格子,这是组合问题,根据组合数学,有C64-8=4.426 * 10 9种;然后是排列问题, 每行每列放一个, 就是0-7的排列, 再筛选,就是8! = 40320,感觉这种很简单了。最后回溯解法, 4! = 24:见代码#includeint tot原创 2015-07-26 08:55:47 · 437 阅读 · 0 评论 -
暴力求解法之枚举排列
#include#includeusing namespace std;int main(){ int n, p[10]; scanf("%d", &n); for(int i = 0; i < n; ++i) scanf("%d", &p[i]); sort(p, p+n); do{ for(int i = 0; i < n ;++原创 2015-07-25 19:35:39 · 434 阅读 · 0 评论 -
暴力求解法之枚举子集
#includevoid print_subset(int n, int s){ for(int i = 0;i < n; ++i) if(s & (1<<i)) printf("%d ", i, s&(1<<i));}int main(){ int n = 4; for(int i = 0; i < (1<<n); ++i) print_subs原创 2015-07-26 08:29:14 · 555 阅读 · 0 评论 -
UVA - 725 Division
//真的是手生了我擦, 以前做过的题, 愣是敲了一节课,醉了#include#include#includeusing namespace std;int k[11];int kk[11];int solve(int n, int m){ k[0] = n/10000;kk[k[0]]++; n %= 10000; k[1] = n/1000;kk[k[1原创 2015-07-25 17:25:58 · 424 阅读 · 0 评论 -
UVA - 10976 Fractions Again?!
#include int Gcd(int a, int b) { return b == 0 ? a : Gcd(b, a%b); } int main() { int k, x, y; while(scanf("%d" ,&k) != EOF) { int cnt = 0; int kkk[1000];原创 2015-07-25 19:23:13 · 297 阅读 · 0 评论 -
UVA - 524 Prime Ring Problem
//这两天做题各种小错误,落空行了。。#include#include#includeusing namespace std;const int maxn = 16 + 5;int C[maxn];int used[maxn];int n;int kase =0;int is_prime(int n){ for(int i = 2; i < n; ++i)原创 2015-07-26 09:44:46 · 424 阅读 · 0 评论 -
UVA - 11059 Maximum Product
//我的dp来。。#include#includeint k[20];int main(){ int n; int kase = 0; while(scanf("%d", &n) != EOF) { for(int i = 1; i <= n; ++i) scanf("%d", &k[i]); long long MAX原创 2015-07-25 18:55:07 · 332 阅读 · 0 评论 -
UVA 211 The Domino Effect
//水回溯//普及一下知识 Domino (多米诺), 两个Pips确定一张Bone原创 2015-09-06 16:59:25 · 463 阅读 · 0 评论