uva 165

回溯  参考了一下别人的解法  1 必须存在  再枚举下一个数字的时候  从当前可取到的最小数字加一枚举到当前可取到的最大数字加一

/*************************************************************************
    > Author: xlc2845       > Mail: xlc2845@gmail.com
    > Created Time: 2013年10月23日 星期三 12时41分39秒
 ************************************************************************/

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#define MAX 0x7fffffff

using namespace std;
int h,k,num[15],now[15],_max,cnt;
bool vis[500];

void cheak(int n, int cur, int va)
{
	if(cur == h)
        return;
	for(int i = 0; i <= n; i++)
    {
        vis[va+now[i]] = true;
		cheak(n, cur+1, va+now[i]);
    }
}

void dfs(int cur, int la)
{
	if(cur >= k)
	{
		if(la > _max)
		{
			_max = la;
			for(int i = 0; i < k; i++)
				num[i] = now[i];
		}
		return;
	}
	for(int i = now[cur-1]+1; i <= la+1; i++)
	{
		now[cur] = i;
		memset(vis, false, sizeof(vis));
		cheak(cur, 0, 0);
		cnt = 1;
		while(vis[cnt]) cnt++;
		dfs(cur+1, cnt-1);
	}
}
int main()
{
    while(scanf("%d%d",&h,&k) == 2 && h+k)
	{
		_max = 0;
		now[0] = 1;
		dfs(1, h);
		for(int i = 0; i < k; i++)
			printf("%3d",num[i]);
		printf(" ->");
		printf("%3d\n",_max);
	}
    return 0;
}




posted @ 2013-10-23 17:07  xlc2845  阅读(130)  评论(0)    收藏  举报