画圆的沙滩

亦简亦美

2011年3月11日 #

数独

摘要: 这是求解数独的程序。还是深搜。class Sudoku { int table[9][9]; bool occupied[9][9][9]; int solution[9][9];public: Sudoku(int* tab = 0) { set(tab); } void set(int* tab) { if (!tab) return; for (int i = 0; i < 81; ++i) table[i/9][i%9] = tab[i]; init(); } int solve() { int cnt = 0; solve(count(), cnt); return cnt; 阅读全文

posted @ 2011-03-11 20:42 acmaru 阅读(178) 评论(0) 推荐(0) 编辑

编辑距离

摘要: 好吧。我还没习惯Live Writer。这部分被覆盖掉了。贴回来。编辑距离,编程之美3.3节。这个问题类似于最长公共子序列LCS,所以可以使用DP和滚动数组来简化计算。int editdist(const string& w1, const string& w2) { size_t len1 = w1.length(), len2 = w2.length(); if (len1++ < len2++) return editdist(w2, w1); int* dist = new int[len2]; for (size_t j = 0; j < len2; ++ 阅读全文

posted @ 2011-03-11 16:33 acmaru 阅读(170) 评论(0) 推荐(0) 编辑

大数计算

摘要: 继续贴一点。大数计算的简单实现,很可惜除法写得很笨拙,用来应付online judge的多数题目应该够了。用string作为存储。string toBigInt(int i) { string r; if (i) { while (i > 0) { r.push_back(i%10 + '0'); i /= 10; } } else { r = "0"; } return r;}void normalize(string& num) { size_t i = num.length(); for (; i > 1 && nu 阅读全文

posted @ 2011-03-11 16:21 acmaru 阅读(156) 评论(0) 推荐(0) 编辑

24点

摘要: 随便写点24点游戏求解程序。两种解法。通过Live Writer 投递,再次测试。编程之美1.16节。关于最后一个扩展问题:引入阶乘运算。对于DP实现,这个不会成为问题。但是,对于深搜,由于阶乘运算不会减少集合的基数,需要进行减枝。其中的一种策略是,当集合中最大的数字除去所有余下的数仍然大于24的话,就可以不必继续了。公共代码:struct Expr { double r; string e; Expr(double rr = 0, const string& ee = ""): r(rr), e(ee) {} bool operator<(const Exp 阅读全文

posted @ 2011-03-11 16:10 acmaru 阅读(284) 评论(0) 推荐(0) 编辑

导航