POJ - 2248 迭代加深

枚举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了啊,待我摸鱼过后再改改

posted @ 2018-02-13 17:16  Caturra  阅读(135)  评论(0编辑  收藏  举报