AcWing170. 加成序列(迭代加深)

AcWing170. 加成序列(迭代加深)

问题

传送门

代码

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 110;
int n;
int path[N];
bool dfs(int u,int k)
{
    if(u==k) return path[u-1] == n;
    bool st[N] = {0};
    for(int i=u-1;i>=0;i--)
    {
        for(int j=i;j>=0;j--)
        {
            int s = path[i] + path[j];
            if(s>n || s<=path[u-1] || st[s])
                continue;
            st[s] = true;
            path[u] = s;
            if(dfs(u+1,k)) return true;
        }
    }
    return false;

}
int main()
{
    path[0] = 1;
    while(cin >> n,n)
    {
        int k = 1;
        while(!dfs(1,k))
            k++;
        for(int i=0;i<k;i++)
        {
            cout << path[i] << " ";
        }
        cout << "\n";
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hh13579/p/12399162.html