【UVa】1374 Power Calculus(IDA*)】的更多相关文章

题目 题目     分析 IDA*大法好,抄了lrj代码.     代码 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxans=14; int n,a[maxans+1]; bool dfs(int d,int maxd) { if(a[d] == n) return true; if(d == maxd) return false;…
题意:给定一个数n,让你求从1至少要做多少次乘除才可以从 x 得到 xn. 析:首先这个是幂级的,次数不会很多,所以可以考虑IDA*算法,这个算法并不难,难在找乐观函数h(x), 这个题乐观函数可以是当前最大数*2maxd - d 小于n,回溯.很好理解,最大的数再一直乘2都达不到,最终肯定达不到. 再就是应该先试乘再试除,还有不要出现负整数.我测了不少知道应该是13次最多,所以这也是一个优化. 为了追求速度,也可以先1~1000的数打表. 代码如下: #include <iostream>…
[题意] 有n个数字的全排列,每次可以剪切一段粘贴到某个位置.问最后变成升序最少多少步. 如"{2,4,1,5,3,6}要2步 {3,4,5,1,2}只要一步 [分析] 迭代深搜真的AC了也觉得慌= = [其实看到这题不应该想到宽搜么??? 全排列只有9!=362880个 这题的IDA*的估价函数特别机智: n<=9,最多2需要8步,深度上限为8. 考虑后继不正确的赎回自个数h,可以证明每次剪切时候h最多减少3,因此当3*d+h>3*maxd时可以剪枝. [证明上面那个画一下就知道,…
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=27&page=show_problem&problem=2549 设w[i,j]为i-j能分割成的最少回文串 f[i]为前i个字符能够分成的最少回文串 w[i,j]=1 当w[i+1,j-1]==1 && s[i]==s[j] 或 i==j-1 && s[i]==s[j] w[i,j]=…
题目 题目     分析 没有估价函数的IDA......     代码 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int q,dx[10]={2,2,-2,-2,1,-1,1,-1},dy[10]={1,-1,1,-1,2,2,-2,-2},ans=1<<15; bool vis[11][11]; int x1,y1,x2,y2; bool…
题目 题目     分析 bfs可以搞,但是我还是喜欢dfs,要记忆化不然会T     代码 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int INF=1<<25; int k,n,m,map[25][25],dx[10]={1,-1,0,0},dy[10]={0,0,1,-1},ans; int vis[25][25][25];…
题目 题目     分析 算是个模拟吧     代码 #include <bits/stdc++.h> using namespace std; map<int,int> a[130]; char s[85]; int n[130]; int PosIs() { int len=strlen(s); for(int i=0;i<len;i++) if(s[i]=='=') return i; return -1; } int get_num(int k) { int res=…
题目 题目     分析 没什么好说的,字符串拼接一下再放进map.其实可以直接开俩数组排序后对比一下,但是我还是想熟悉熟悉map用法. 呃400ms,有点慢.     代码 #include <bits/stdc++.h> using namespace std; int read(string a) { int ans=0,i=0; while(i<a.length()){ans=(ans<<3)+(ans<<1)+a[i]-'0';i++;} return…
UVA10298:https://www.luogu.org/problemnew/show/UVA10298 思路 设P[x]数组为 前x个字符的最大前缀长度等于后缀字串 由P数组的定义我们可以知道 对于给定的长度为n字符串 则n-P[n]所在位置就是这个字符串的重复最长子串的最后一个字符的位置 如果这个字符串真的是由其中的一个子串循环而成 那么它的长度肯定是n-P[n]的倍数 因此我们用KMP预处理出给定字符串的所有P数组 再判断n是否整除n-P[n]即可 如果整除ans=n/(n-P[n]…
题目链接 当guess>limit-deep的时候return就好了. guess是估价函数,值为不在自己地盘上的骑士个数.limit是本次迭代阈值.deep是已经走了多少步. 这个优化是显然的.因为一次跳跃最多可以复原一个骑士.假设最好的情况,所有的骑士都能一步跳回去,如果这样还不能在阈值步数内复原,那不论如何都没法复原了——也就没有继续搜下去的必要了. 放上代码 #include<cstdio> #include<cstdlib> #include<cstring&…