【POJ 2248】 Addition Chain
2024-08-26 14:43:21
【题目链接】
http://poj.org/problem?id=2248
【算法】
搜索剪枝
剪枝1 : 优化搜索顺序,从大到小枚举
剪枝2 : Ai + Aj可能相等,只需搜一次即可
剪枝3 : 通过观察发现 : m <= 10,可以用迭代加深搜索
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std; int i,n,step;
int ans[]; inline bool dfs(int dep)
{
int i,j;
bool visited[];
if (dep > step)
{
if (ans[step] == n) return true;
else return false;
} else
{
memset(visited,false,sizeof(visited));
for (i = dep - ; i >= ; i--)
{
for (j = i; j >= ; j--)
{
if (ans[i] + ans[j] <= ans[i-]) break;
if (ans[i] + ans[j] > n || visited[ans[i]+ans[j]]) continue;
visited[ans[i]+ans[j]] = true;
ans[dep] = ans[i] + ans[j];
if (dfs(dep+)) return true;
}
}
return false;
}
} int main()
{ while (scanf("%d",&n) != EOF && n)
{
ans[] = ;
for (i = ; i <= ; i++)
{
step = i;
if (dfs()) break;
}
for (i = ; i <= step; i++) printf("%d ",ans[i]);
printf("\n");
} return ; }
最新文章
- 【代码笔记】iOS-获得富文本设置以后的文字高度
- 使用matlab进行空间拟合
- 用C#开发ActiveX控件,并使用web调用
- 单元测试_JUnit4的应用与实践
- Linux下C程序插入执行shell脚本
- sqlserver 动态表名 动态字段名 执行 动态sql
- jQuery修改操作css属性实现方法
- Tomcat无法部署项目
- 数据库基础学习3-T-SQL语句
- ELK 日志系统搭建配置
- Problem F: 分数类的类型转换
- JS脚本-零星片段
- 微信小程序开发——使用回调函数出现异常:TypeError: Cannot read property &#39;setData&#39; of undefined
- Syslog和Windows事件日志收集
- 使用Eclipse Memory Analyzer分析Tomcat内存溢出
- .NetCore中使用AspectCore、ExceptionLess 实现AOP操作日志记录
- Explore Basic Behavior of the TurtleBot ---3
- Hadoop MapReduce执行过程实例分析
- HDU-4850 Wow! Such String! (构造)
- java的反射应用