题目传送门 | |
---|---|
题意:搜索出一个符合要求的最短序列
优化搜索顺序,从大到下枚举;
排除等效情况 设置标记数组
#include<iostream>
using namespace std;
int n;
const int N=150;
int path[N];
int depth;
bool dfs(int u)
{
if(u-1==depth) return path[u-1]==n;
bool st[N]={false};
for(int i=u-1;i>=1;i--){
for(int j=i;j>=1;j--){
int s=path[i]+path[j];
if(s>=path[u-1] && s<=n && !st[s])
{
st[s]=true;
path[u]=s;
if(dfs(u+1)) return true;
else{
st[s]=false;
}
}
}
}
return false;
}
int main()
{
while(cin>>n,n)
{
if(n==1) cout<<'1'<<endl;
else{
path[1]=1;
depth=2;
for(;depth<=10;depth++)
{
if(dfs(2))
{
for(int i=1;i<=depth;i++) cout<<path[i]<<' ';
cout<<endl;
break;
}
}
}
}
return 0;
}