摘要:r排列生成: gen 递归层数d表示正在生成第d个元素。 vis记录是否出现过。 #include#include#includeusing namespace std;int n, r;int A[50], vis[50];//记录第i个元素是否生成过int cnt;int rer;void output(int r){ for(int i = 0; i #include...
阅读全文
摘要:// 题意: // 输入两个整数N, H,按照字典序输出所有长度为N,恰好包含H个1的01串 // 规模:1#include const int maxn = 20;int N, H, bits[maxn];// 从bits[d]开始确定,已经用了c0个0和c1个1void gen(int d, int c0, int c1) { if(d == N) { if(c1 ...
阅读全文
摘要:比牌先比牌型,大的牌型大于小的牌型,牌型一般分为10种,从大到小为: 同花大顺(Royal Flush):最高为Ace(一点)的同花顺。 例: 同花顺(Straight Flush):同一花色,顺序的牌。 例: 四条(Four of a Kind,亦称“铁支”、“四张”或“炸弹”):有四张同一点数的牌。 例: 满堂红(Fullhouse,亦称“俘虏”、“骷髅”、“夫...
阅读全文
摘要:// 题意:有P个LED灯,以及N个字符,要求选出个数最少的LED灯,使得即使只有这些灯正常工作,也能区分出这N个字符 // 题意抽象:输入两个整数P, N以及N行P列的01矩阵,找少的列,能区分所有行 // 规模:P#include#include#include#include#includeusing namespace std;const int P=15;const i...
阅读全文
摘要:Problem G. Birthday Cake Background Lucy and Lily are twins. Today is their birthday. Mother buys a birthday cake for them.Now we put the cake onto a Descartes coordinate. Its center is at (...
阅读全文
摘要:bfs图的时候需要注意判重。 八数码状态数 9!=362880 ,如果用9维的vis,9^9=387420489,会有很大的浪费。 有三种方法来解决空间浪费问题: 1、编码解码 这里用cantor编码 typedef int State[9];const int MAXSTATE = 1000000;State st[MAXSTATE], goal;int dist[MAXSTAT...
阅读全文
摘要:http://zh.wikipedia.org/wiki/康托展开 http://www.nocow.cn/index.php/康托展开 http://blog.sina.com.cn/s/blog_4bf7b6580100l2zs.html http://www.skymoon.biz/?p=86 http://www.cnblogs.com/1-2-3/archive/2011/04/...
阅读全文
摘要:例7-3 倒水问题。 有装满水的6升的杯子、空的3升杯子和1升杯子,3个杯子中都没有刻度。在不使用其他道具的情况下,是否可以量出4升的水呢? 方法如图7-7所示。 图7-7 倒水问题:一种方法是(6,0,0)→(3,3,0)→(3,2,1)→(4,2,0) 注意:由于没有刻度,用杯子x给杯子y倒水时必须一直持续到把杯子y倒满或者把杯子x倒空,而不能中途停止。 你的任务是解决一般性...
阅读全文
摘要:通过swap方式生成的全排列并不是字典顺序的: #include#include#include#include#includeusing namespace std;int A[20];int cnt;int recur;void perm(int cur, int n, int *A){ recur++; if(cur==n)//empty { ...
阅读全文
摘要:1、增量构造法: 一次选出一个元素放到集合中,由于集合中的元素是无序的,所以我们从小到大生成所有元素。每次选择的元素都要比之前的大。 如上图,生成 {1, 2, 3} 的子集过程中的解答树。 共有2^n个节点(8个),每个节点都是解。 2、位向量法 每次有选和不选两种情况 {1 ,2 ,3} 子集的解答数 总共有 1+2+4+ … +2^n= 2^(n+1)...
阅读全文
摘要:7.2.1 生成1~n的全排列 #includeint A[100];// 输出1~n的全排列void print_permutation(int n, int* A, int cur) { int i, j; if(cur == n) { // 递归边界 for(i = 0; i #include#include#include#includeusing namespace ...
阅读全文
摘要:学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时) #include#include#include#include#includeusing namespace std;const int N=31;const int dl[]={-...
阅读全文
摘要:bfs 最短路径 #include#include#include#include#include#includeusing namespace std;const int N=105;int n, m, rs, cs, rt, ct;typedef int A[N][N];A maze, vis, dist, last_dir;int dr[] = {-1,1,0,0};int dc...
阅读全文
摘要:// 题意:输入标准国际象棋棋盘上的两个格子,求马最少需要多少步从起点跳到终点 BFS求最短路: bfs并维护距离状态cnt, vis记录是否访问过 #include#include#include#include#include#includeusing namespace std;int r1, c1, r2, c2;const int N=8;int vis[N][N];...
阅读全文
摘要:// 题意:输入一个迷宫,从*开始遍历,把可达点标记为字符# 注意迷宫边界不规则,要用strlen判断。 #include#include#include#include#includeusing namespace std;const int maxn = 100 + 5;char maze[maxn][maxn];int dr[]={0, 0, -1, 1};int dc[]=...
阅读全文
摘要:// 题意:给一个图案,其中'.'表示背景,非'.'字符组成的连通块为筛子。每个筛子里又包含两种字符,其中'X'组成的连通块表示筛子上的点 // 统计每个筛子里有多少个“X”连通块 思路:两次dfs //思路:先dfs找包含X和*的区域,再在*的区域中dfs X的个数#include#include#include#include#include#includeusing n...
阅读全文
摘要:技巧:遍历8个方向 for(int dr = -1; dr #include#include#include#includeusing namespace std;const int N=102;char buf[N][N];int m, n;int cnt;int dr[]={0, 0, 1, 1, 1, -1, -1, -1};int dc[]={1, -1, -1, 0, 1,...
阅读全文
摘要:注意点: 空树情况处理。 循环时候可以先判断符合条件,再递减: while(i-1>=0 && buf[r+2][i-1]=='-') i--; #include#include#include#include#includeusing namespace std;const int N=200+2;char buf[N][N];int n;//递归遍历并且输出以字符buf[...
阅读全文
摘要:我的解法: 建树,递归判断 #include#include#include#include#includeusing namespace std;struct Node { Node() { wl=wr=dl=dr=0; l=r=0; } int wl; int dl...
阅读全文
摘要:// UVa699 The Falling Leaves // 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位。从左向右输出每个水平位置的所有结点的权值之和。按照递归方式输入,-1表示空 树 // UVa699 The Falling Leaves// Rujia Liu// 题意:给一棵二叉树,每...
阅读全文
摘要:// UVa712 S-Trees// Rujia Liu// 题意:给一棵满二叉树,每一层代表一个01变量,取0时往左走,取1时往右走。给出所有叶子的值,以及一些查询(即每个变量的值),求最后到达的叶子的值// 算法:结点从上到下编号为1, 2, 3, ...则左走就是乘以2,右走是乘以2加1。第一个叶子的编号是2^n#include#includeusing namespace std;...
阅读全文