回溯  参考了一下别人的解法  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;
}

uva 165的更多相关文章

  1. uva 165 Stamps

    题意: 现有k种邮票面额, 一封信上最多贴h张邮票. 求能贴出的最大连续区间,即[1, max_value]这个区间内的所有面额都能贴出来. 并输出k种面额, h + k <= 9. 思路: 这 ...

  2. UVA 165 Stamps (DFS深搜回溯)

     Stamps  The government of Nova Mareterrania requires that various legal documents have stamps attac ...

  3. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

  4. UVA 1412 Fund Management (预处理+状压dp)

    状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...

  5. Fast Matrix Operations(UVA)11992

    UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y ...

  6. [百度网盘]Xamarin for Visual Studio 3.7.165 Preview 最新版-介绍

    Xamarin 3.7.165 Preview 下载地址:http://download.xamarin.com/XamarinforVisualStudio/Windows/Xamarin.Visu ...

  7. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  8. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  9. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

随机推荐

  1. CSS之perspective

    <!DOCTYPE html> <html> <head> <style> #div1 { position: relative; height: 15 ...

  2. Xcode-项目模板修改

    项目模板就是创建工程的时候选择的某一个条目, Xcode会根据选择的条目生成固定格式的项目 例如想创建一个命令行项目就选择Command Line Tool 如何修改项目模板 1.应用程序中,找到Xc ...

  3. 学习之spring属性文件注入

    package com.my.proper; import org.springframework.beans.factory.annotation.Value; import org.springf ...

  4. 笔试面试题-小米Git

    题目描述: git是一种分布式代码管理工具,git通过树的形式记录文件的更改历史,比如: base'<--base<--A<--A' ^ | --- B<--B' 小米工程师常 ...

  5. WaitForSingleObject用法

    对应函数 编辑 VC声明 DWORD WaitForSingleObject( HANDLE hHandle, DWORD dwMilliseconds );   参数 编辑 hHandle[in]对 ...

  6. 模板:多Case输入处理

    利用cin实现 while(cin >> value) { } 调试时使用Ctrl + Z 输入文件结束符

  7. JS远程获取网页源代码的例子

    js代码获取网页源代码. 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < ...

  8. 《PHP与MySQL WEB开发》读书笔记

    <PHP与MySQL WEB开发>读书笔记 作者:[美]Luke Welling PHP输出的HereDoc语法: echo <<<theEnd line 1 line ...

  9. 008.ComputeReplacement

    Delphi function ComputeReplacement: UTF8String; 类型:function 可见性:public 所在单元:System.RegularExpression ...

  10. 《C和指针》读书笔记——第五章 操作符和表达式

    1.当/操作符的两个操作数都是整数时,它执行整除运算:其他都是执行浮点数除法. 2.逻辑移位:左边移入的位用0填充: 算数移位:左边移入的位用符号位填充: 3.位置1 :value |= 1<& ...