Addition Chains POJ - 2248(迭代加深搜索)

题意:传送门
题解:对于这个题需要找出符合长度最小的长度,对于 100 100 100这个数而言, 1 + 2 + 4 + 6 + 10 + 16 + 26 + 42 > 100 1+2+4+6+10+16+26+42>100 1+2+4+6+10+16+26+42>100,所以有答案的情况大概在 10 10 10以内就存在了,可以直接使用迭代加深搜索,当到达某个深度搜到了答案时就是最短长度了,当然对于每个搜索内部还可以派出等效冗余,如果 x [ i ] + x [ j ] x[i]+x[j] x[i]+x[j]是相等的,搜索多次也是无用,使用 b o o l bool bool数组进行标记,加以判重,避免重复搜索某一个和。
c o d e : code: code:

#include<iostream>
#include<cstdio>
using namespace std;
const int N=1e2+50;
int n,path[N];
bool dfs(int u,int depth)
{
    if(u==depth)return path[u-1]==n;
    bool st[N]={false};
    for(int i=u-1;i>=0;i--){
        for(int j=u-1;j>=0;j--){
            int t=path[i]+path[j];
            if(t<=n&&t>path[u-1]&&!st[t]){
                st[t]=true;
                path[u]=t;
                if(dfs(u+1,depth))return true;
            }
        }
    }
    return false;
}
int main()
{
    while(cin>>n&&n){
        path[0]=1;
        int depth=1;
        while(!dfs(1,depth))depth++;
        for(int i=0;i<depth;i++)cout<<path[i]<<" ";
        cout<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值