1. 程式人生 > >POJ - 2248 叠代加深

POJ - 2248 叠代加深

dep blog 短鏈 body pos ios bre poj bool

枚舉ak的值
反向枚舉使ak盡快到達最短鏈的n

/*H E A D*/
int n,m,a[23333],dep;
bool dfs(int x){
    if(a[x-1]>n||a[x-1]<=a[x-2])return 0;
    if(x>dep){
        if(a[x-1]==n) return 1;
        else return 0;
    }
//  rep(i,1,x-1){
//      rep(j,1,x-1){
    rrep(i,x-1,1){
        rrep(j,x-1,i){
            a[x]=a[i]+a[j];
            if
(dfs(x+1))return 1; } } return 0; } int main(){ IOS; while(cin>>n){ if(n==0)break; if(n==1){ cout<<1<<endl; continue; } a[1]=1; rep(depth,2,oo){ dep=depth; if(dfs(2)){ rep(i,1
,depth-1) cout<<a[i]<<" "; cout<<a[depth]<<endl; break; } } } return 0; }

UPD:UVa上的TLE了啊,待我摸魚過後再改改

POJ - 2248 叠代加深