
+ 搜索
文章平均质量分 70
codekun
这个作者很懒,什么都没留下…
展开
-
UVa 211 - The Domino Effect [DFS剪枝]
这道题只看书上的中文解释可能有些费力,理解题后还是比较简单的,对每个数字看能否与右边或下边的数字配对组成一张牌,注意加各种剪枝就行。#include using namespace std;int kase, solution;int tab[10][10], res[10][10], vis[30];int dict[7][7];const int dir[2][2] = { {原创 2015-02-13 13:49:47 · 766 阅读 · 0 评论 -
UVA - 10129 - Play on Words <欧拉道路+并查集>
Play on WordsSome of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzz原创 2015-01-27 11:53:15 · 602 阅读 · 0 评论 -
HDU - 1010 Tempter of the Bone 简单DFS+剪枝
Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 75924 Accepted Submission(s): 20768Problem DescriptionThe原创 2015-01-05 23:07:56 · 528 阅读 · 0 评论 -
UVA - 536 Tree Recovery
Tree Recovery Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.This is a原创 2014-12-11 08:23:18 · 437 阅读 · 0 评论 -
UVA - 129 Krypton Factor
Krypton Factor You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental and physical abilities. In one section of the contest原创 2014-12-15 21:54:59 · 471 阅读 · 0 评论 -
UVA - 524 Prime Ring Problem
Prime Ring Problem A ring is composed of n (even number) circles as shown in diagram. Put natural numbers into each circle separately, and the sum of numbers in two adjacent circles s原创 2014-12-15 17:02:02 · 686 阅读 · 0 评论 -
UVa 818 - Cutting Chains <位运算+搜索>
用二进制枚举要开环的位置,然后判断是否成环,是否为链,开环数是否>=链数-1。调试了很久才过的,并且时间复杂度并不太好,还能优化。#include using namespace std;int n, g[16][16], vis[16], l, kase = 0;bool line(int k){ for(int i = 0; i < n; ++i){原创 2015-02-22 16:40:15 · 908 阅读 · 0 评论 -
UVa 12166 - Equilibrium Mobile <二叉树+DFS>
A mobile is a type of kinetic sculpture constructed to take advantage of the principle of equilibrium. It consists of a number of rods, from which weighted objects or further rods hang. The objects ha原创 2015-01-28 00:14:12 · 2552 阅读 · 0 评论 -
UVa 1103 - Ancient Messages [进制转换+DFS]
提供三组测试数据吧:5 3ffff0fffff0ffff5 50fff00f0f0fffff00f0000f005 50f0f00f0f0fffff00f0000f00 0 0AC输出是:Case 1: KCase 2: ACase 3: W做题的时候,发现使用ios::sync_with_st原创 2015-02-28 13:00:14 · 4228 阅读 · 3 评论 -
HDU - 1232 畅通工程 <并查集>
Problem Description某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路? Input测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N原创 2015-01-19 11:26:09 · 603 阅读 · 0 评论 -
UVA - 439 Knight Moves
Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set原创 2014-12-11 09:37:40 · 566 阅读 · 0 评论 -
HDU 2553 - N皇后问题 [回溯法]
Problem Description在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上。你的任务是,对于给定的N,求出有多少种合法的放置方法。#include #include #include using namespace std;int N, cnt, vis[3][32], res[12]原创 2015-03-06 11:41:43 · 632 阅读 · 0 评论 -
HDU 1016 - Prime Ring Problem [简单DFS]
不想说这题有多么水了,最简单的DFS题了,但是!我花了N天在杭电用G++交了20多遍一直超时,最后改C++交过了!!这是什么情况!!无语了!!#define _CRT_SECURE_NO_WARNINGS#include #include #include const int maxn = 1024;bool checked[maxn] = { 1, 1 };int pri[原创 2015-03-17 23:16:52 · 405 阅读 · 0 评论 -
HDU 1285 - 确定比赛名次 <拓扑排序>
这题本来想按照算法竞赛入门经典上的DFS来做,但是无奈这个题有点特殊,有重边的情况,所以学习了另外一种复杂度相同切更好理解的一种算法。#include #include #include #include #define maxn 512using namespace std;int n, m, cnt;bool G[maxn][maxn], vis[maxn];int re原创 2015-02-03 12:21:35 · 477 阅读 · 0 评论 -
UVa 11853 - Paintball <图论+DFS>
这道题仍然是按照书上的思路来做,从上往下以DFS来搜索判断上下是否连通。根据圆与左右两边的下交点来判断出入点。#include using namespace std;struct dot{ double x, y, r;}all[1024];int n, vis[1024], ok;double in, out;void DFS(int u){ if(!ok原创 2015-02-04 11:21:28 · 1187 阅读 · 0 评论 -
UVa 208 - Firetruck <双向DFS>
这题后台数据一定很坑,估计是那种从节点1可以扩展到节点2~19,但是就是无法与节点20连通的那种,才导致了刚开始没有逆向搜索的时候TLE到3000MS。逆向DFS找到所有与终点相连的节点保存下来,速度很快的。这样正向搜索时就要把与终点不相连的节点剪枝。判断连通性的方法有很多,其实本题还可以用BFS或者并查集防止超时。#include #include #include #incl原创 2015-02-07 17:16:20 · 564 阅读 · 0 评论 -
UVa 12118 - Inspector's Dilemma <欧拉道路+DFS>
这道题题意比较好理解,但需要一些关于欧拉道路的思考。刚开始考虑用并查集做,每个集(或者说每条链)有可能是环状,或者有多个奇点,这就需要多个入口和出口,需要统计,用并查集并不太好实现。然后考虑DFS的做法,需要计算每个节点的度数,统计每条链的奇点个数,并且注意每条连至少需要一个入口和一个出口,最后将多条链合并即可。#include using namespace std;int v,原创 2015-02-02 11:46:39 · 1867 阅读 · 2 评论 -
UVA - 140 Bandwidth
Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the elements in V, then the bandwidth of a node vis defined as the maximum dista原创 2014-12-16 16:06:37 · 484 阅读 · 0 评论 -
UVA 1600 Patrol Robot
A robot has to patrol around a rectangular area which is in a form of mxn grid (m rows and n columns). The rows are labeled from 1 to m. The columns are labeled from 1 to n. A cell (i, j)denotes the原创 2014-12-11 18:26:41 · 998 阅读 · 0 评论 -
UVa 1374 - Power Calculus <IDA*算法>
本题值得学习的地方就是乐观估计函数的推到,需要数学指数的运算,另外计算2的n次方的时候可以用1另外对于当前的集合存储,用C数列存储再合适不过了,刚开始用set来存,对数据进行增删浪费了大量时间,而且操作起来特别麻烦。#include using namespace std;int n, maxd, res[32];bool DFS(int d){ if(d == max原创 2015-02-07 14:25:11 · 605 阅读 · 0 评论 -
UVA - 10305 Ordering Tasks
Problem FOrdering TasksInput: standard inputOutput: standard outputTime Limit: 1 secondMemory Limit: 32 MB John has n tasks to do. Unfortunately, the tasks are not independent and the ex原创 2014-12-09 16:46:11 · 402 阅读 · 0 评论 -
UVa 225 – Golygons [DFS+剪枝]
距离100题还有4道,此时最忌讳急于求成,所以打算做题不看题解,有时候一道题很难需要想很多甚至做好几天都不能AC,但正是自己不断修改错误,优化代码,才使得自己的编程水平有进步,最后的AC才更加有成就感。这道题思路还算简单,但是需要加各种剪枝条件,如当前做到达的地方能否再走n-d步到达原点,我感觉像是A*算法,提前估计当前状态还需要扩展几层才能得到目标状态。经历了三次TLE,总结一下原创 2015-02-10 13:06:56 · 650 阅读 · 0 评论 -
UVa 11212 - Editing a Book <状态空间搜索+IDA*算法>
本题的关键是应用IDA*算法,降低时间复杂度,启发函数的发现和推导是重点。对于可以用回溯法求解但解答树的深度没有明显上限的题目,可以考虑使用迭代加深搜索。设计一个乐观估计函数,预测从当前节点至少还学要扩展几层才有可能得到解,则状态空间搜索变成了IDA*算法。刚开始吧状态定义为typedef int State[12],但后来发现编译器老是报错并且极易出现各种错误,索性直接定义为一个st原创 2015-02-06 20:19:14 · 591 阅读 · 0 评论 -
UVa 1572 - Self-Assembly <图论模型+拓扑排序>
这道题不看书上的分析还真不知道怎么做,关键就是转化成图论,然后利用拓扑排序判断DAG来做。另外一个值得学习的地方是编号的时候,A+和A-可分别变为2n+1和2n,然后一个重要的关系要利用好就是(2n+1)^1 = 2n,2n^1 = (2n+1),可以很容易的进行A+和A-的变换。#include #define maxn 64using namespace std;int n,原创 2015-02-03 21:48:06 · 882 阅读 · 0 评论 -
UVa 1599 - Ideal Path <两次BFS>
两个月前就搞这道题,昨天翻出来直接修改的当时的超时代码,经历了多种错误,最后终于还是AC了。这道题还是挺好的一道题,需要正反两次BFS,需要加各种防超时的判断。实际上还是考差对问题的理解,什么时候应该剪枝,什么地方不必重复判断。第一次反着BFS,找到各点到终点的最短距离。第二次正着BFS,顺着距离递减的方向,按照颜色小的优先的方法走。#include #include #inc原创 2015-02-03 15:05:17 · 1439 阅读 · 0 评论 -
HDU - 1856 - More is better <并查集>
//本题用cin超时啊,要用scanf#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include #include using namespace std;const int maxn = 10000001;int pre[maxn];int topcnt[maxn];int F原创 2015-01-19 12:32:37 · 456 阅读 · 0 评论 -
HDU - 1213 - How Many Tables <并查集>
Problem DescriptionToday is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the fri原创 2015-01-19 11:58:35 · 524 阅读 · 0 评论 -
UVa 12558 - Egyptian Fractions (HARD version) [IDA*]
简单版和加强版的区别就是数据范围扩大到了long long,并且加了限制条件。用IDA*算法即可解决。#include using namespace std;typedef long long LL;LL maxd, ans[10240], v[10240];set ban;LL gcd(LL a, LL b){ return b ? gcd(b, a % b) : a原创 2015-02-24 15:00:56 · 768 阅读 · 0 评论 -
UVa 1218 Perfect Service [DFS+DP]
题意:给定一个树形的机器结构,安装服务器,每台服务器恰好跟一台服务器相邻,问最少装几台服务器。首先DFS把树建立起来,然后动规求解,注意设置无穷大inf后累加要防止超int范围。#define _CRT_SECURE_NO_WARNINGS#include #include #include #include #include #include #include using原创 2015-04-14 11:11:34 · 592 阅读 · 0 评论