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;
}
uva 165的更多相关文章
- uva 165 Stamps
题意: 现有k种邮票面额, 一封信上最多贴h张邮票. 求能贴出的最大连续区间,即[1, max_value]这个区间内的所有面额都能贴出来. 并输出k种面额, h + k <= 9. 思路: 这 ...
- UVA 165 Stamps (DFS深搜回溯)
Stamps The government of Nova Mareterrania requires that various legal documents have stamps attac ...
- 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。
这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...
- UVA 1412 Fund Management (预处理+状压dp)
状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...
- Fast Matrix Operations(UVA)11992
UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y ...
- [百度网盘]Xamarin for Visual Studio 3.7.165 Preview 最新版-介绍
Xamarin 3.7.165 Preview 下载地址:http://download.xamarin.com/XamarinforVisualStudio/Windows/Xamarin.Visu ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
随机推荐
- C# WinForm打开IE浏览器并访问网址
C# WinForm 打开浏览器并访问网址代码: System.Diagnostics.Process.Start("iexplore.exe", "http://kel ...
- windows系统下在dos命令行kill掉被占用的pid
备忘 1.开始-->运行-->cmd 2.命令行输入: netstat -ano I findstr 端口(被占用的端口号) 3.输入: tasklist | findstr 端口(获取步 ...
- FileZilla ftp服务器安装
在官网下载server版本的FileZilla,我下载的是(The latest stable version of FileZilla Server is 0.9.41) 然后直接安装就可了,我由于 ...
- c++ algorithm 的用法
1 , accumulate()template<class _II, class _Ty> inline_Ty accumulate(_II _F, _II _L, _Ty _V){fo ...
- Java数字格式化输出时前面补0
Java数字格式化输出时前面补0 星期日 2014年11月30日| 分类: Java /** * 里数字转字符串前面自动补0的实现. * */ public class TestString ...
- java应用uploadify 3.2丢失session
java应用uploadify 3.2丢失session http://c-bai.iteye.com/blog/1829269 uploadify上传用的是一个flash插件. flash中有个bu ...
- php 实现 mysql数据表优化与修复
<?php $link = mysql_connect("localhost", "root", "") or die("e ...
- zencart后台增加菜单选项
如果要在程序中使用额外的参数,在后台控制,添加到菜单属性 在后台 SQL脚本 运行如下 SQL语句 INSERT INTO configuration (configuration_title, co ...
- PHP中session的使用
1.初始化(使用session前都要使用,一个页面用一个就可以了) session_start(); 2.保存 $_SESSION[$sessionName]=$value; (value可以是dou ...
- (转载)用SQL语句创建Access表
<来源网址:http://www.delphifans.com/infoview/Article_220.html>用SQL语句创建Access表 很久以前弄的,用了一天的时间,没有什么技 ...