
算法与数据结构
vecri
算法,软件,持之以恒
展开
-
zju1092
类似于Floyd算法的动态规划#include #include using namespace std;/*#include ifstream fin("data.txt");#define cin fin*/int n, m;char name[31][100];double g[31][31];void cal原创 2009-07-31 22:39:00 · 430 阅读 · 0 评论 -
pku3211 Washing Clothes(动态规划)
类似0-1背包问题的动态规划 #include using namespace std;/*#include ifstream fin("data.txt");#define cin fin*/int M, N;char colors[10][12];int clothes[10][101];int clothnum[10];原创 2009-11-18 21:31:00 · 585 阅读 · 0 评论 -
pku3740 Easy Finding (搜索)
给一个M*N的矩阵,矩阵元素均为0, 1问是否可以选出一些行,使得这些行的每一行有且仅有一个1例如 中的1, 4, 5行满足这个条件这里的数据规模比较小,直接深度搜索就可以。 #include #include bool mat[16][300];bool ans, flag[300];int n, m;void dfs(int d原创 2009-11-19 23:54:00 · 558 阅读 · 0 评论 -
pku2312 坦克大战 (搜索)
经典游戏坦克大战, 问从位置YOU到位置TARGET的最少步数,类似迷宫的问题 #include #include using namespace std;int m, n, srcx, srcy;char tmap[300][300];struct POINT{ int x; int y; int sth; //0为无障碍,原创 2009-11-20 12:37:00 · 583 阅读 · 0 评论 -
pku2157 Maze (搜索)
迷宫中有钥匙和门,收集到钥匙能打开对应的门,问能否走出迷宫,这里的算法是多次广度优先搜素,每次广度优先搜索收集钥匙,看能否走到目的地,如果走不到目的地,则拿出收集到的钥匙打开门,继续广度优先搜索。 如果钥匙收集完了,到不了目的地,也打不开新的门,说明无解。 #include #include using namespace std;int keynum[5], ke原创 2009-11-20 21:47:00 · 620 阅读 · 0 评论 -
pku1699 Best Sequence (搜索)
求多个子串的最短父串, #include #include using namespace std;char str[10][22];bool flag[10];char res[220];int ans, n;void search(int depth, int curlen){ if (curlen > ans) re原创 2009-11-20 21:56:00 · 633 阅读 · 0 评论 -
pku2138 Travel Games (搜索)
比较简单的搜索,主要是决定任意两个字符串之间是否有题目所要求的关系 #include #include #include using namespace std;vector adj[1000];char str[1000][84];int len[1000];int first, D;char fstr[84];int a原创 2009-11-20 23:16:00 · 592 阅读 · 0 评论 -
pku1036 Gangsters (动态规划)
dp[i][j]表示当第i个海盗进门时门的状态为k,这时获得的最多钱 #include #include using namespace std;int n, k, time, r;struct INFO { int T; int P; int S;} info[102];int dp[101][101]; //dp[i][j原创 2009-11-22 00:18:00 · 654 阅读 · 0 评论 -
pku1063 Flip and Shift
#include using namespace std;int pos[31];int m, n, loop;int main(){ int t, i, j, k, sum1, sum2, odd, even; cin >> t; while (t--) { cin >> loop; for (m=0, n=0, i=0; i<loop;原创 2009-11-22 00:28:00 · 504 阅读 · 0 评论 -
pku1204 Word Puzzles (Trie)
trie, 递归和非递归的查询都实现了一下 #include #include using namespace std;char str[1010][1010];int L, C, W;int srcx, srcy;int dir[8][2] = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1,原创 2009-11-30 11:41:00 · 725 阅读 · 0 评论 -
pku2002 Squares (折半查找)
枚举正方形的一条边,查找满足条件的另外两个顶点是否存在,使用折半查找 #include #include using namespace std;struct POINT { int x; int y;} ps[1001];int n;bool comp(POINT p1, POINT p2){ if (p1.x != p2.x原创 2009-11-30 14:43:00 · 504 阅读 · 0 评论 -
pku1816 Wild Words (trie)
a-z与?*的匹配,利用trie来实现的 #include #include #include using namespace std;vector res;const int kind=28;//字母种类struct Treenode//树的结点结构{ vector ids;//这个附加变量在本题中记录遍历到该结点形成的字符串出现的次数,原创 2009-11-30 16:33:00 · 645 阅读 · 0 评论 -
pku2001 Shortest Prefixes (trie)
trie, 查找区别其他字符串的最短前缀。 #include #include char str[1010][25];int n;const int kind=26; //字母种类struct Treenode //树的结点结构{ int count; //这个附加变量在本题中记录遍历到该结点形成的字符串出现的次数,在不同题中可记录原创 2009-11-30 12:40:00 · 707 阅读 · 0 评论 -
解一次模互质同余方程组
pku1006, 解一次模互质同余方程组x=p mod 23;x=e mod 28;x=i mod 33;求x #include #include using namespace std;int gcd(int m, int n) // 用户必须保证m,n的合法性!{ int temp; while( m%n != 0 )原创 2009-12-04 14:59:00 · 955 阅读 · 0 评论 -
pku1887 最长递减子序列
利用动态规划来计算最长递减子序列。dp[i]为以h[i]结尾的最长递减子序列的长度 #include using namespace std;int h[10000];int dp[10000];#define max(a,b) ((a)>(b)?(a):(b))int main(){ int len, next, ans, i, j, cou原创 2009-11-18 21:04:00 · 842 阅读 · 0 评论 -
Flip Game翻转游戏
4*4的棋盘,棋盘上的每个位置都是黑或白,每个位置的颜色可以翻转,并且翻转一个位置的同时其周围的棋子的颜色也同时翻转。给一个目标状态,问多少步可以到达目标状态。 广度优先搜索。 #include #include using namespace std;struct STATE { bool map[4][4]; int step;};STAT原创 2010-01-14 14:42:00 · 851 阅读 · 0 评论 -
pku2153 Rank List (STL的运用)
#include #include #include #include using namespace std;int grade[10001];int stunum, examnum;char name[10001][32];int main(){ map msi; map::iterator ii; int i, j,原创 2009-11-18 22:24:00 · 561 阅读 · 0 评论 -
zju1093
简单的DP状态是h[i][j], 代表顶面是第i块的第j面时的最高高度状态转移很简单#include using namespace std;/*#include ifstream fin("data.txt");#define cin fin*/int n;int blocks[31][3];int faces[3][3] =原创 2009-07-31 22:41:00 · 445 阅读 · 0 评论 -
zju1103
简单的bfs,状态表示为3个pieces的坐标,状态转移就是寻找可用的边走下去,注意到1 2 3和3 2 1是一个坐标,也就是说3个pieces的坐标是对称的所以将这3个数排序,以减少状态总数 #include #include #include using namespace std;/*#include ifstream fin("原创 2009-08-04 15:02:00 · 447 阅读 · 0 评论 -
zju1161
枚举+贪心在取钓鱼数最多的湖时候可以用优先队列#include #include using namespace std;/*#include ifstream fin("data.txt");#define cin fin*/int n, h, fi[26], ti[26], di[26];int timepath[26原创 2009-08-05 09:04:00 · 397 阅读 · 0 评论 -
pku1276 Cash Machine (DP)
动态规划,n个数的线性组合注意其中的逆序更新, flag[i]表示这n个数的线性组合结果能否为i每次更新从0到end之间的部分#include using namespace std;/*#include ifstream fin("data.txt");#define cin fin*/int dk[11], nk[11], n, cash原创 2009-08-25 15:50:00 · 476 阅读 · 0 评论 -
pku1014 Dividing (DP)
动态规划,和pku1276的算法是基本一样的显然,如果总价值为奇数,则均分是不可能的,如果总价值为偶数,则看各种marble的线性组合值能否为总价值的一半,如果可能,则可均分#include using namespace std;/*#include ifstream fin("data.txt");#define cin fin*/int mar原创 2009-08-25 20:20:00 · 175 阅读 · 1 评论 -
pku1019
Number SequenceTime Limit: 1000MSMemory Limit: 10000KTotal Submissions: 16917Accepted: 4450DescriptionAsingle positive integer i is given. Write a prog原创 2009-10-29 19:30:00 · 920 阅读 · 0 评论 -
pku1239 Increasing Sequences (动态规划)
Increasing SequencesTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 1584 Accepted: 662DescriptionGivena string of digits, insert commas to cr原创 2009-11-02 18:09:00 · 1813 阅读 · 1 评论 -
pku2385 Apple Catching (动态规划)
比较简单的DP, 利用滚动数组,代码如下: #include using namespace std;int dp[2][31][2];int t, w;int tree[1001];#define max(a, b) ((a)>(b) ? (a):(b))int main(){ int i, j, k, ans, sel; while原创 2009-11-09 21:27:00 · 455 阅读 · 0 评论 -
pku3639 Exchange Rates (动态规划)
Exchange RatesTime Limit: 1000MSMemory Limit: 65536KTotal Submissions: 1942Accepted: 647DescriptionNowthat the Loonie is hovering about par with原创 2009-11-09 20:23:00 · 1096 阅读 · 0 评论 -
pku3638 Moogle (动态规划)
题目地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=3638 题目大意:一个地图软件需要需要存储h个房子的坐标信息,但为节约存储空间可以只存储其中的c个房子的坐标信息,其他房子的坐标信息可以由插值得到。问怎样选择这个c个房子,使得所有房子的插值误差之和最小, 输出最小的插值误差均值。 题目分析:动态规划,状态定义为: dp[i][j]表原创 2009-11-09 20:44:00 · 724 阅读 · 0 评论 -
pku1972 Dice Stacking
比较简单的题在第一个骰子放法固定之后,第二个骰子的底面值必须和第一个骰子的顶面值一样,所以第二个骰子的底面是一定的,将与底面相邻的4个面的最大值转出来,同理,将第三个骰子的与底面相邻的4个面中的最大值转出来,这样可以得到第一个骰子放置方式固定后的面最大值。第一个骰子的放置方式有6种,取这6个放置方式的面最大值就是解。代码比较WS。。 #include #include原创 2009-11-15 01:28:00 · 546 阅读 · 0 评论 -
pku1882 Stamps (动态规划)
题目大意是求n种面值的邮票能连续组合出的最大值。如面值1和3的邮票取5张可以组合出1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15但1-13中间是连续的,所以连续组合出的最大值为13采用的算法为动态规划 #include using namespace std;bool flag[1002];int deno[10];i原创 2009-11-17 00:25:00 · 615 阅读 · 0 评论 -
pku3253 Fence Repair
利用构建赫夫曼树的贪心思想来维护一个小顶堆 #include #include using namespace std;int N;int woods[20001];int main(){ int i, j; while (cin >> N) { for (i=0; i<N; i++) cin >> woods[i];原创 2009-11-17 13:37:00 · 544 阅读 · 0 评论 -
uva100 The 3n + 1 problem
记忆化搜索,就是说如果f[n]已经计算过,直接使用, 另外注意数组越界问题。为了加快查询速度,使用RMQ, 即区间最值。用动态规划的方法来查询某个区间的最值。pku1207也是这道题目,不过规模变小了很多。 #include #include #define MAXN 1000001unsigned long f[MAXN];unsigned long原创 2009-12-04 15:10:00 · 663 阅读 · 0 评论