POJ2248 Addition Chains 迭代加深

不知蓝书的标程在说什么,,,,于是自己想了一下。。。发现自己的代码短的一批。。。



限制搜索深度+枚举时从大往小枚举,以更接近n+bool判重,避免重复搜索

#include<cstdio>
#include<iostream>
#include<cstring>
#define R register int
using namespace std;
inline int g() {
    R ret=0; register char ch; while(!isdigit(ch=getchar()));
    do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret;
}
int n,dep=1;
int a[110];
bool v[110];
inline bool dfs(int crt) {
    if(crt==dep+1) return a[dep]==n;
    for(R i=crt-1;i>=1;--i) for(R j=i;j>=1;--j) {
        if(a[i]+a[j]>n) continue;
        if(a[i]+a[j]<=a[crt-1]) break;
        if(v[a[i]+a[j]]) continue;
        a[crt]=a[i]+a[j]; if(dfs(crt+1)) return true;
    } return false;
}
signed main() {
    while(n=g(),n!=0) { dep=1;
        memset(a,0,sizeof(a));memset(v,false,sizeof(v)); a[1]=1;
        while(!dfs(2)) ++dep;
        for(R i=1;i<=dep;++i) printf("%d ",a[i]); putchar(‘\n‘);
    }
}


2019.04.26

原文地址:https://www.cnblogs.com/Jackpei/p/10772315.html

时间: 2024-10-28 19:33:42

POJ2248 Addition Chains 迭代加深的相关文章

poj2248 Addition Chains 迭代加深搜索

Addition Chains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5454   Accepted: 2923   Special Judge Description An addition chain for n is an integer sequence <a0, a1,a2,...,am="">with the following four properties: a0 = 1 a

POJ2248 Addition Chains

Addition Chains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5273   Accepted: 2833   Special Judge Description An addition chain for n is an integer sequence with the following four properties: a0 = 1 am = n a0 < a1 < a2 < ... <

C++解题报告 : 迭代加深搜索之 ZOJ 1937 Addition Chains

此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/articles/10767945.html 我们可以这么想,设当前规定长度为M,题目要求得出的数为N. 在搜索中,当前的步数为step,当前的数列为 数组a. 首先来确定思路,便是在以得出的数列a中枚举每两个数相加得出sum,然后继续搜索下一步. 初步的代码便是: void iddfs(int x) { for

poj 3134 Power Calculus(迭代加深dfs+强剪枝)

Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications: x2 = x × x, x3 = x2 × x, x4 = x3 × x, …, x31 = x30 × x. The operation of squaring can be appreciably shorten the sequence of multiplications.

[poj 2331] Water pipe ID A*迭代加深搜索(dfs)

Water pipe Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2265 Accepted: 602 Description The Eastowner city is perpetually haunted with water supply shortages, so in order to remedy this problem a new water-pipe has been built. Builders s

UVA 10160 Servicing Stations(状态压缩+迭代加深)

[题目链接] LInk [题目大意] 给出一些点和边,选择一个点就能把这个点和相邻的点都覆盖,求最小点覆盖 [题解] 我们压缩点被覆盖的状态,迭代加深搜索覆盖的最小点数, 当剩余的点全部选上时都无法完全覆盖就剪枝. [代码] #include <cstdio> #include <algorithm> using namespace std; typedef long long LL; const int N=36; int i,n,m,x,y,limit; LL st[N],Lf

UVA-11214 Guarding the Chessboard (迭代加深搜索)

题目大意:在一个国际象棋盘上放置皇后,使得目标全部被占领,求最少的皇后个数. 题目分析:迭代加深搜索,否则超时. 小技巧:用vis[0][r].vis[1][c].vis[2][r+c].vis[c-r+N]分别标志(r,c)位置相对应的行.列.主.副对角线有没有被占领(详见<入门经典(第2版)>P193),其中N表示任意一个比行数和列数都大(大于等于)的数. 代码如下: # include<iostream> # include<cstdio> # include&l

USACO/fence8 迭代加深搜索+剪枝

题目链接 迭代加深搜索思想. 枚举答案K,考虑到能否切出K个木头,那么我们当然选最小的K个来切. 1.对于原材料,我们是首选最大的还是最小的?显然,首选大的能够更容易切出,也更容易得到答案. 2.对于目标木头,我们是优先得到最大的还是最小的?显然,由于K个木头我们都要得到,那么当然先把最大的(最难得到的)先得到,这种搜索策略更优. 3.假设总原材料为all,前K个木头总和为sum,那么all-sum就是这一次切割过程中能[浪费]的最大数目.对于一个切剩下的原材料,若它比最小的目标木头还要小,则它

hdu 1560 DNA sequence(迭代加深搜索)

DNA sequence Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 15   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description The twenty-first century