SPOJ 370 Ones and zeros BFS + 同余剪枝】的更多相关文章

题意:给一些n,求出最小的只包含0,1的n的倍数 设两数a, b满足: a < b 并且a % n = b % n. 如果 ( a * 10^x + c ) % n = z , 根据同余定理,( b * 10^x + c ) % n 也等于 z. b的情况其实与a相同,如果a不符合条件,那么b一定不符合条件. 因此我们在搜索时,从1开始,每次往后添加0或1,如果得到的数与之前得到的某数同余,则扔掉,否则放入队列继续搜索. #include <cstdio> #include <cs…
没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib> ; struct node { int mod; int fa; int digit; node() {} node( int mod, int fa, int dig ):mod(mod), fa(fa), digit(dig) { } }; int N; ]; bool vis[MAXN]; nod…
Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no mo…
题目大意: 有一堆积木,0号节点每次可以和其上方,下方,左上,右下的其中一个交换,问至少需要多少次达到目标状态,若步数超过20,输出too difficult 目标状态: 0 1 1 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 题目分析: 因为前段时间做了一道转花盆刻骨铭心,所以一看到这题就开始bfs+hash,明知道过不了,但谁知道姿势正确得了85分,后来出题人告诉数据最大步数才14,我把搜索停止条件改成了18,瞬间ac... 正解有很多种:迭代加深,双向搜索,\(…
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1104 在做这道题目一定要对同余定理有足够的了解,所以对这道题目对同余定理进行总结 首先要明白计算机里的取余计算和数学里的不一样的,计算机里的负数取余可以是负数的.例如-1%11=-1 而数学里的取余是-1%11=10 同余定理: 若a对d取余,和b对d取余的结果是相等的,那么称a,b对d是同余的.记作a≡b(mod d);这是数学里的定义. 下面看同余定理的几个性质: 1,a≡a(mod d) 数字…
J - Goblin Wars Time Limit:432MS    Memory Limit:1572864KB    64bit IO Format:%lld & %llu SubmitStatusPracticeSPOJ AMR11J Description The wizards and witches of Hogwarts School of Witchcraft found Prof. Binn's History of Magic lesson to be no less bo…
http://172.20.6.3/Problem_Show.asp?id=1442 想到最短路的简直神了,如果我写我大概只能写一个30分的bfs. 从数据范围可以看出思路是bfs剪枝,但这里的剪枝是通过最短路的预处理实现的. 设需要移动的格子为a格子. 对求最小移动数有意义的移动只有两种,一种是空白格子的移动,一种是a格子移动到空白格子里. 可以得知要把空白格子移动到a格子旁边然后对a格子进行移动. 那么我们有了每次查找时进行的预处理1:求空白格子到a格子四周格子的最短路. 在模拟移动的过程中…
Alien's Necklace Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1526    Accepted Submission(s): 415 Problem Description JYY is taking a trip to Mars. To get accepted by the Martians, he decid…
Mines Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1110    Accepted Submission(s): 280 Problem Description Terrorists put some mines in a crowded square recently. The police evacuate all peo…
通过几道经典BFS例题阐述BFS思路 ZOJ2913-Bus Pass 题意:找一个center区域,使得center到所有公交线路最短,有等距的center则输出id最小的. 题解:经典的BFS,由公交线路最多只经过10*20个区域,而总区域数可达10^5个,因此应该从公交线路通过队列一层层向外扩展,最后判断一次center的位置即可. //选定一个center使得其到所有公交线路最短 //对所有公交线路进行BFS(公交线路比其他区域少得多) //Time:150Ms Memory:2840K…