09 2015 档案

UVA-10384 The Wall Pushers (IDA*)
摘要:题目大意:走迷宫,遇到墙时可以推着墙走,但墙后还是墙时便不能推。求出一条任意的最短路径。题目分析:这道题出的比较人性,输入的时候便是将四周的墙用二进制数表示好了,其实这样减轻了做题人的负担。IDA*,当到最近的一个出口的距离加上当前层数cur都比maxd大时,则剪枝。不过,值得注意的是:当推着墙走的... 阅读全文

posted @ 2015-09-29 15:37 20143605 阅读(509) 评论(1) 推荐(0)

UVA-1601 The Morning after Halloween(BFS或双向BFS)
摘要:题目大意:在一张图中,以最少的步数将a,b,c移到对应的A,B,C上去。其中,每个2x2的方格都有障碍并且不能两个小写字母同时占据一个格子。题目分析:为避免超时,先将图中所有能联通的空格建起一张图,然后再BFS。代码如下:# include# include# include# include# i... 阅读全文

posted @ 2015-09-28 19:50 20143605 阅读(910) 评论(0) 推荐(0)

UVA-129 Krypton Factor(回溯)
摘要:题目大意:由字母A到Z组成的字符串,其中有两个子串完全相同的叫做容易的串,反之叫困难的串。找出由前L个字母组成的第n个困难的串。题目分析:简单回溯,不过要判断是否存在重复子串比较棘手。《入门经典》上借鉴八皇后问题,只判断添进字符后是否存在连续子串。具体做法是这样的,以长度为对象枚举以新添进字符为尾巴... 阅读全文

posted @ 2015-09-28 15:46 20143605 阅读(358) 评论(0) 推荐(0)

HDU-5492 Find a path (枚举+DP)
摘要:Problem DescriptionFrog fell into a maze. This maze is a rectangle containing N rows and M columns. Each grid in this maze contains a number, which is... 阅读全文

posted @ 2015-09-28 11:51 20143605 阅读(389) 评论(3) 推荐(0)

UVA-11212 Editing a Book (IDA*)
摘要:题目大意:将一个数字序列以最少的剪切次数粘贴成另一个数字序列。题目分析:很显然,最坏的情况是需要n-1次剪切,搜索层数不多,但每一层的状态数目又非常庞大,适宜使用IDA*。考虑每一个序列后续不正确的数字个数h,每一次剪切最多3个数字后续数字发生改变,每次剪切最多可减少3个不正确的后续数字。所以在当前... 阅读全文

posted @ 2015-09-28 10:38 20143605 阅读(541) 评论(0) 推荐(0)

UVA-307 Sticks (DFS+剪枝)
摘要:题目大意:用n根长度未必相等的木棒匹配出最多数量的等长木棒。题目分析:枚举所有可能的等长木棒的长度,通过DFS的方式逐根匹配,在此过程中要剪枝。先将木棒长度按从大到小排序,也就是说匹配每一根等长木棒时总是优先挑选长的。剪枝方案如下:1. 若第i-1根木棒在当前方案的匹配中没有用到并且length[i... 阅读全文

posted @ 2015-09-25 11:31 20143605 阅读(594) 评论(0) 推荐(1)

UVA-1343 The Rotation Game (IDA*)
摘要:题目大意:数字1,2,3都有八个,求出最少的旋转次数使得图形中间八个数相同。旋转规则:对于每一长行或每一长列,每次旋转就是将数据向头的位置移动一位,头上的数放置到尾部。若次数相同,则找出字典序最小旋转次序。题目分析:IDA*,若当前在第cur层,中间八个数中1,2,3的个数分别为a,b,c。则d=8... 阅读全文

posted @ 2015-09-24 00:19 20143605 阅读(443) 评论(0) 推荐(0)

UVA-10603 Fill (BFS)
摘要:题目大意:有三个已知体积但不知刻度的杯子,前两个杯子中初始时没有水,第三个装满水,问是否可以倒出d升水,如果倒不出,则倒出一个最大的d’,使得d’# include# include# include# includeusing namespace std;struct Node{ int v... 阅读全文

posted @ 2015-09-23 11:34 20143605 阅读(457) 评论(0) 推荐(0)

UVA-140 Bandwidth (回溯+剪枝)
摘要:题目大意:求一个使带宽最小的排列和最小带宽。带宽是指一个字母到其相邻字母的距离最大值。题目分析:在递归生成全排列的过程中剪枝,剪枝方案还是两个。一、当前解不如最优解优时,减去;二、预测的理想解不必最优解优时,减去。将与当前最后一个位置上的字母相邻的字母全部接过来,便得理想解。代码如下:# inclu... 阅读全文

posted @ 2015-09-23 09:20 20143605 阅读(221) 评论(0) 推荐(0)

UVA-10976 Fractions Again?!
摘要:题目大意:给一个k,寻找出所有使1/k=1/a+1/b(a>b)成立的所有a和b。题目分析:枚举b,从k+1枚举到2*k。代码如下:# include# include# include# includeusing namespace std;int v[10000];int main(){ ... 阅读全文

posted @ 2015-09-22 10:35 20143605 阅读(113) 评论(0) 推荐(0)

UVA-1374 Power Calculus (迭代加深搜索)
摘要:题目大意:问最少经过几次乘除法可以使x变成xn。题目分析:迭代加深搜索。代码如下:# include# include# include# include# includeusing namespace std;int num[20],maxn,cnt;bool dfs(int d,int maxd... 阅读全文

posted @ 2015-09-22 10:04 20143605 阅读(520) 评论(0) 推荐(0)

UVA-11882 Biggest Number (DFS+剪枝)
摘要:题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次。题目分析:DFS。剪枝方案:在当前的处境下,找出所有还能到达的点的个数,若当前数字的长度加上个数仍小于目前最优答案的长度,则剪去;若长度相等,则将所有还能到达的数字... 阅读全文

posted @ 2015-09-21 20:18 20143605 阅读(890) 评论(0) 推荐(1)

UVA-12569 Planning mobile robot on Tree (EASY Version) (BFS+状态压缩)
摘要:题目大意:一张无向连通图,有一个机器人,若干个石头,每次移动只能移向相连的节点,并且一个节点上只能有一样且一个东西(机器人或石头),找出一种使机器人从指定位置到另一个指定位置的最小步数方案,输出移动步骤。题目分析:以机器人的所在位置和石头所在位置集合标记状态,状态数最多有15*2^15个。广搜之。代... 阅读全文

posted @ 2015-09-21 15:38 20143605 阅读(233) 评论(0) 推荐(0)

UVA-11214 Guarding the Chessboard (迭代加深搜索)
摘要:题目大意:在一个国际象棋盘上放置皇后,使得目标全部被占领,求最少的皇后个数。题目分析:迭代加深搜索,否则超时。小技巧:用vis[0][r]、vis[1][c]、vis[2][r+c]、vis[c-r+N]分别标志(r,c)位置相对应的行、列、主、副对角线有没有被占领(详见《入门经典(第2版)》P19... 阅读全文

posted @ 2015-09-20 22:31 20143605 阅读(285) 评论(0) 推荐(0)

UVA-12558 Egyptian Fractions (HARD version) (IDA* 或 迭代加深搜索)
摘要:题目大意:经典的埃及分数问题。代码如下:# include# include# include# includeusing namespace std;# define LL long longint num[5],a,b,k;LL ans[10000],v[10000];LL gcd(LL a,L... 阅读全文

posted @ 2015-09-20 20:59 20143605 阅读(405) 评论(0) 推荐(0)

Largest Point (2015沈阳赛区网络赛水题)
摘要:Problem DescriptionGiven the sequence A with n integers t1,t2,⋯,tn. Given the integral coefficients a and b. The fact that select two elements ti and ... 阅读全文

posted @ 2015-09-20 09:46 20143605 阅读(203) 评论(0) 推荐(0)

hihoCoder-1087 Hamiltonian Cycle (记忆化搜索)
摘要:描述Given a directed graph containing n vertice (numbered from 1 to n) and m edges. Can you tell us how many different Hamiltonian Cycles are there in t... 阅读全文

posted @ 2015-09-18 22:30 20143605 阅读(379) 评论(0) 推荐(0)

UVA-12113 Overlapping Squares (回溯+暴力)
摘要:题目大意:问能不能用不超过6张2x2的方纸在4x4的方格中摆出给定的图形?题目分析:暴力枚举出P(9,6)种(最坏情况)方案即可。代码如下:# include# include# include# includeusing namespace std;int vis[10];char mp[8][1... 阅读全文

posted @ 2015-09-17 21:21 20143605 阅读(821) 评论(0) 推荐(0)

UVA-818 Cutting Chains (位压缩+暴力搜索)
摘要:题目大意:一种环能打开和闭合。现在有n(1# include# include# include# includeusing namespace std;int n,ans,st[15],s[15],vis[15];int bitCount(int sta){ return sta==0?0:... 阅读全文

posted @ 2015-09-17 15:39 20143605 阅读(920) 评论(0) 推荐(0)

UVA-208 Firetruck (回溯)
摘要:题目大意:给一张无向图,节点编号从1到n(n# include# include# includeusing namespace std;int mp[25][25],vis[25],ans;bool ok(int s,int e){ if(s==e) return true; ... 阅读全文

posted @ 2015-09-15 20:43 20143605 阅读(141) 评论(0) 推荐(0)

UVA-1252 Twenty Questions (状压DP)
摘要:题目大意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同)最小需要多少次询问?题目分析:定义dp(s,a)表示询问了的特征集合为s,物体含有特征集合a中的所有特征,但不含特征集合 s^a 中的所有特征时还需... 阅读全文

posted @ 2015-09-15 12:13 20143605 阅读(482) 评论(0) 推荐(0)

HDU-5001 Walk (概率DP)
摘要:Problem DescriptionI used to think I could be anything, but now I know that I couldn't do anything. So I started traveling.The nation looks like a con... 阅读全文

posted @ 2015-09-11 13:45 20143605 阅读(175) 评论(0) 推荐(0)

UVA-1220 Party at Hali-Bula (树的最大独立集)
摘要:题目大意:数的最大独立集问题。特殊在要求回答答案是否唯一。题目分析:定义状态dp(i,1),dp(i,0)分别表示以i为根节点的子树选不选i最多可选的人数,f(i,1),f(i,0)分别表示以i为根节点的子树选不选i的方案唯一性。则当选i时,i的子节点都不能选,否则,可选可不选,因此状态转移方程如下... 阅读全文

posted @ 2015-09-08 22:14 20143605 阅读(157) 评论(0) 推荐(0)

UVA-12186 Another Crisis (树状DP)
摘要:题目大意:一家工厂,一个老板(编号为0),n个工人(编号1~n),其中,有的工人是中层领导,管辖一部分其他工人。现在大家要签署一份加薪申请书,但是按照规定不能越级上访,所以只能通过一层层的中间领导传到老板手中。当某个中间领导的手下签名员工人数达到 m% 时,他也会签上自己的名字。为确保申请书顺利到达... 阅读全文

posted @ 2015-09-08 15:16 20143605 阅读(203) 评论(0) 推荐(0)

UVA-1626 Brackets sequence (简单区间DP)
摘要:题目大意:给一个有小括号和中括号组成的序列,满足题中的三个条件时,是合法的。不满足时是不合法的,问将一个不合法的序列最少添加几个括号可以使之变成合法的。输出最短合法序列。题目分析:这是《入门经典》上的一道例题。如果仅让求最短序列是极简单的,定义dp(i,j)表示将区间 i~j 变为合法添加的最小字符... 阅读全文

posted @ 2015-09-08 11:37 20143605 阅读(201) 评论(0) 推荐(0)

UVA-10003 Cutting Sticks (区间DP)
摘要:题目大意:将一段长为L的木棒在给定的n个切割点切开,每切一次的花费等于当前木棒的长度。求切成n+1段的最小花费。题目分析:区间DP。定义dp(i,j)表示切割区间i~j的花费,则 f(i,j)=min(f(i,k)+f(k,j))+dist(i,j)。时间复杂度为n3。代码如下:# include#... 阅读全文

posted @ 2015-09-07 21:56 20143605 阅读(189) 评论(0) 推荐(0)

HDU-4849 Wow! Such City! (单源最短路)
摘要:Problem Description Doge, tired of being a popular image on internet, is considering moving to another city for a new way of life. In his country th... 阅读全文

posted @ 2015-09-06 20:45 20143605 阅读(175) 评论(0) 推荐(0)

HDU-4856 Tunnels (BFS+状压DP)
摘要:Problem DescriptionBob is travelling in Xi’an. He finds many secret tunnels beneath the city. In his eyes, the city is a grid. He can’t enter a grid w... 阅读全文

posted @ 2015-09-06 19:00 20143605 阅读(198) 评论(0) 推荐(0)

HDU-4850 Wow! Such String! (构造)
摘要:Problem Description Recently, doge starts to get interested in a strange problem: whether there exists a string A following all the rules below: 1.T... 阅读全文

posted @ 2015-09-06 14:43 20143605 阅读(175) 评论(0) 推荐(0)

HDU-4848 Wow! Such Conquering! (回溯+剪枝)
摘要:Problem Description There are n Doge Planets in the Doge Space. The conqueror of Doge Space is Super Doge, who is going to inspect his Doge Army on al... 阅读全文

posted @ 2015-09-03 22:46 20143605 阅读(349) 评论(0) 推荐(0)