- 博客(17)
- 收藏
- 关注
原创 POJ3009-Curling 2.0
外星人打冰球的故事。 这是一道深搜的题,与一般的题不同之处在于它并不是走一格,而是走一条直线路径。 #include #include #include using namespace std; const int MAXN = 20; int map[MAXN+2][MAXN+2]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1,
2016-03-31 21:22:20 460
原创 POJ1979-Red and Black
图的深搜。 #include #include const int MAXN = 20; char map[MAXN+2][MAXN+2]; int cnt = 0; int w, h; void Dfs(int x, int y) { cnt++; map[x][y] = '#'; if (x - 1 >= 0 && map[x-1][y] == '.
2016-03-31 17:02:49 266
原创 POJ3253-Fence Repair
将题目想成一棵二叉树,一块木板为父节点,切割后的两段长度为它的子节点。 如此切割的开销为父节点木板对应的长度。 总开销为所有含有子节点的父节点对应长度只和。 等于这棵树的各叶节点乘以节点的深度。 因此最短的板应是深度最大的叶子节点之一。 最短的板与次短的板的节点是兄弟节点。 将l按大小顺序排序,最短的板l1和次短的板l2是由长度为(l1+l2)的板得到的。 递归地将这n-1块木板的问
2016-03-31 16:00:06 389
原创 POJ3617-Best Cow Line
给定长度为N的字符串S,要求构造一个字典序最小的字符串T。 #include const int MAXN = 2000; char s[MAXN+2]; char t[MAXN+2]; int main() { int n; scanf("%d", &n); getchar(); int i; for (i = 0; i < n; i+
2016-03-31 09:55:21 266
原创 POJ2386-Lake Counting
#include const int MAXN = 100; char field[MAXN+2][MAXN+2]; int n, m; void Dfs(int x, int y) { if (field[x][y] == 'W') { field[x][y] = '.'; for (int dx = -1; dx <= 1; dx
2016-03-30 16:47:41 283
原创 POJ1852-Ants
买了本《挑战程序设计竞赛》,打算静下心好好啃完一本书。 “事实上,可以知道两只蚂蚁相遇后,当它们保持原样交错而过继续前进也不会有任何问题。” #include #include #include using namespace std; const int MAXN = 1000000; int loc[MAXN]; int main() { int t; scan
2016-03-30 15:34:49 244
原创 BestCoder Round #77 (div.2)
1001 单元素集合所有子集异或后是它本身,其他集合异或后答案都为0; #include int ctn[1002]; int main() { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { int n; scanf("%d", &n);
2016-03-27 14:22:37 273
原创 数论-快速幂
快速幂算法通过递归减小幂运算的规模。 long int Pow(long int X, unsigned int N) { if (N == 0) { return 1; } // if (N == 1) { // return X; // } if (!(N % 2)) { //N为偶数
2016-03-25 20:22:10 274
原创 排序-快速排序
快速排序顾名思义即是很快的排序(妈的智障....) 快速排序的版本很多。 取中快排,选取中间值将数组划分为左右两部分。 void midqsort(int a[], int left, int right) { int i = left, j = right; int mid = a[(left + right) / 2]; int temp; wh
2016-03-25 15:21:58 285
原创 数论-斐波那契数列
斐波那契数列可以由递归,迭代以及数学计算得到。 #include #include using namespace std; int f[1000]; int Fib1(int n) { if (n <= 1) { return n; } else return Fib1(n-1)+Fib1(n-2); } int Fib2(i
2016-03-25 14:13:05 332
原创 数论-素数判定
用于快速判断一个数是否为素数。 bool isPrime(int num) { if (num == 2 || num == 3) return true; if (num % 6 != 1 && num % 6 != 5) return false; for (int i = 5; i * i <= num; i += 6
2016-03-25 14:03:48 387
原创 简单数论-Gcd
欧几里德算法是最早的算法,用辗转相除法求最大公因数。 #include using namespace std; //Gcd(m, n) == Gcd(n, m mod n) //Gcd(m, 0) == m int Gcd(int m, int n) { while (n != 0) { int r = m % n; m = n;
2016-03-25 14:01:22 536
原创 埃拉托色尼(Eratosthenes)筛法
筛选法求素数 #include #include using namespace std; const int MAXN = 1200; int A[MAXN]; int L[MAXN]; //sieve prime list void Sieve(int n) { int p; for (p = 2; p < n; p++) A[p] = p;
2016-03-25 13:57:46 790
原创 HDUOJ2553-N皇后问题
对于N皇后问题首先的代码 #include int IsLegal[3][25]; int cnt = 0; int cmd; void Dfs(int n) { if (n == cmd + 1) cnt++; else { for(int i = 1; i <= cmd; i++) { if(IsLegal[0][
2016-03-24 21:33:14 446
原创 HDUOJ1042-N!
大数阶乘问题 #include #include using namespace std; const int MAXN = 40001; int a[MAXN] = {0}; void BigFactorial(int m) { int i, j; int carry; int temp; a[MAXN - 1] = 1; for (
2016-03-23 22:02:25 307
原创 Dfs回溯-八皇后问题
八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例。该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。 将棋盘的每一行作为搜索树的节点。建立一个数组IsLegal[3][18]分别记录列,主对角线,辅对角线的情况,判段皇后的位置是否合法。 主对角线方向上
2016-03-23 21:26:50 503
原创 POJ1164-The Castle
将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为: Dfs(V) { if (v访问过) { return; } 将v标记为访问过; 对和v相邻的每个点u:Dfs(u); } int main() { while (在图中能找到未访问过的点k) { Dfs(k); } } Poj
2016-03-20 13:13:57 604
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人