POJ2248 Addition Chains 迭代加深
不知蓝书的标程在说什么,,,,于是自己想了一下。。。发现自己的代码短的一批。。。
限制搜索深度+枚举时从大往小枚举,以更接近n+bool判重,避免重复搜索
#include<cstdio>
#include<iostream>
#include<cstring>
#define R register int
using namespace std;
inline int g() {
R ret=; register char ch; while(!isdigit(ch=getchar()));
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret;
}
int n,dep=;
int a[];
bool v[];
inline bool dfs(int crt) {
if(crt==dep+) return a[dep]==n;
for(R i=crt-;i>=;--i) for(R j=i;j>=;--j) {
if(a[i]+a[j]>n) continue;
if(a[i]+a[j]<=a[crt-]) break;
if(v[a[i]+a[j]]) continue;
a[crt]=a[i]+a[j]; if(dfs(crt+)) return true;
} return false;
}
signed main() {
while(n=g(),n!=) { dep=;
memset(a,,sizeof(a));memset(v,false,sizeof(v)); a[]=;
while(!dfs()) ++dep;
for(R i=;i<=dep;++i) printf("%d ",a[i]); putchar('\n');
}
}
2019.04.26
POJ2248 Addition Chains 迭代加深的更多相关文章
- [POJ2248] Addition Chains 迭代加深搜索
Addition Chains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5454 Accepted: 2923 ...
- POJ 2248 - Addition Chains - [迭代加深DFS]
题目链接:http://bailian.openjudge.cn/practice/2248 题解: 迭代加深DFS. DFS思路:从目前 $x[1 \sim p]$ 中选取两个,作为一个新的值尝试放 ...
- poj 2248 Addition Chains (迭代加深搜索)
[题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- Addition Chains POJ - 2248 (bfs / dfs / 迭代加深)
An addition chain for n is an integer sequence <a0, a1,a2,...,am=""> with the follow ...
- C++解题报告 : 迭代加深搜索之 ZOJ 1937 Addition Chains
此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/article ...
- [題解](迭代加深)POJ2248_Addition Chains
發現m不會特別大,也就是層數比較淺,所以採用迭代加深 由於xi+xj可能相同,所以開一下vis數組判斷重複 #include<iostream> #include<cstdio> ...
- 「一本通 1.3 例 4」Addition Chains
Addition Chains 题面 对于一个数列 \(a_1,a_2 \dots a_{m-1},a_m\) 且 \(a_1<a_2 \dots a_{m-1}<a_m\). 数列中的一 ...
- UVA 529 Addition Chains(迭代搜索)
Addition Chains An addition chain for n is an integer sequence with the following four propertie ...
随机推荐
- 680. Valid Palindrome II 对称字符串-可删字母版本
[抄题]: Given a non-empty string s, you may delete at most one character. Judge whether you can make i ...
- 1-element.src.match("bulbon")
element.src.match("bulbon")这里的math里面”bulbon“是什么意思? 原代码中 if (element.src.match("bulbon ...
- CF 961G Partitions
推不动式子 我们考虑每一个$w_i$对答案的贡献,因为题目中定义集合的价值为$W(S) = \left | S \right |\sum_{x \in S}w_x$,这个系数$\left | S \r ...
- Ajax——三种数据传输格式
一.HTML HTML由一些普通文本组成.如果服务器通过XMLHTTPRequest发送HTML,文本将存储在responseText属性中. 从服务器端发送的HTML的代码在浏览器端不需要用Java ...
- WinAPI多线程
WIN32线程控制主要实现线程的创建.终止.挂起和恢复等操作,这些操作都依赖于WIN32提供的一组API和具体编译器的C运行时库函数.在启动一个线程之前,必须为线程编写一个全局的线程函数,一般来说,C ...
- Which Uri Encoding method should i use in C#/.net?
June 19, 2015 This too is one of the boring "factual" posts. Sorry Lachlan. I never know w ...
- AutoLayout自动布局之VFL语言代码实现(一个神奇的语言)
一.什么是VFL语言?为什么要VFL语言? VFL全称是Visual Format Language,翻译过来是“可视化格式语言” VFL是苹果公司为了简化Autolayout的编码而推出的抽象语言 ...
- Network in Network(NiN)
- Mlpconv layer with "micronetwork" with each conv layer to compute more abstract features ...
- easyui SWFUpload
业务背景:实现一个用药人的增加功能,用药人信息中包含附件.如题所示,主要讨论easyui上传的实现.jsp页面代码(弹出框),一个简单的增加页面 div id=addMedicationDlg cla ...
- Java Script 脚本的几种基本格式:
1. <script> document.Write("Hello wrrld!!!"); </script> 2. <scrip ...