poj3134迭代加深搜索
2016-05-05 14:31
176 查看
题目大意:给你一个n,让你从x出发只用乘除法,求最小的次数算出x^n,所有的使用乘方必须已知即曾经计算出来。
思路:迭代加深搜索。n不超过1000,所以最深出现答案的层数不会太深,可以试用跌代加深搜索。即每次设定搜索层数,判断该层是否有解。若有解输出改成即可。需要剪枝操作。
思路:迭代加深搜索。n不超过1000,所以最深出现答案的层数不会太深,可以试用跌代加深搜索。即每次设定搜索层数,判断该层是否有解。若有解输出改成即可。需要剪枝操作。
#include <algorithm> #include <set> #include <cstdio> #include <cstring> #include <string> #include <queue> #include <stack> #include <iostream> using namespace std; #define maxn 1000010 #define mem(a , b) memset(a , b , sizeof(a)) #define LL long long int val[maxn]; int pos , n; bool dfs(int num , int depth) { if(num > depth) return false; if(val[pos] == n) return true; if(val[pos] << (depth - num) < n) return false; pos ++ ; for(int i = 0 ; i < pos ; i ++) { val[pos] = val[pos-1] + val[i]; if(dfs(num + 1, depth)) return true; val[pos] = abs(val[pos-1] - val[i]); if(dfs(num + 1, depth)) return true; } pos -- ; return false; } int main() { int t; while(scanf("%d" , &n) != EOF && n) { int i; for(i = 0 ; ; i ++) { val[pos = 0] = 1; if(dfs(0 , i)) break; } cout << i << endl; } return 0; }
相关文章推荐
- Android实现多线程断点下载
- Spark流处理解读(2)结合RDD讲解原理
- 父元素与子元素之间的margin-top问题
- 20135327郭皓--读书笔记六
- Selenium实战脚本集(4)--简单的开发者头条客户端
- 网页病毒挂马原理解析
- sqlserver2005 导出包含数据的脚本
- Android Volley 框架的使用
- 狗脸识别APP整合
- 多文件预览上传基于Struts
- JavaScript 分页组件定义
- liunx使用node搭建web sevice
- iOS开发------手动约束布局出现NSAutoresizingMaskLayoutConstraint冲突(解决)
- 新的站点挂到百度
- POJ 1258 Agri-Net(最小生成树)
- 学习资料
- mysql: 三个范式
- 旅行商问题 (状态压缩DP)
- Andorid简单调试步骤
- iOS UIButton 自定义图片和文字位置详解