zoukankan      html  css  js  c++  java
  • UVA 165 Stamps

    UVA_165

    由于h+k<=9,数据量不是很大,所以可以直接枚举、深搜。

    深搜函数一共有两个,其中一个用来枚举能够连续达到的值,如果通过已有面值可以拼出当前值,则可以继续保持已有面值的状态并继续深搜,或者可以创造一个新的面值等于当前值并继续深搜,当两种手段都不能达到拼出当前值的目的时,当前值减1即为现有面值的状态下能够拼出的最大值;另一个深搜函数用来做判断是否可以通过已有面值拼出当前值的工作,只需要依次选择是否选用当前面值的邮票并使总张数小于h即可,当某一时刻可以拼出当前面值时返回1,所有情况都不能拼出当前面值时则返回0

    #include<stdio.h>
    #include
    <string.h>
    int H,K,ans[15],res;
    int judge(int target,int cur,int num,int tot,int *A,int end)
    {
    int i,j;
    if(tot==target)
    return 1;
    else if(cur==end||num==H)
    return 0;
    if(judge(target,cur+1,num,tot,A,end))
    return 1;
    if(judge(target,cur,num+1,tot+A[cur],A,end))
    return 1;
    return 0;
    }
    void dfs(int cur,int *A,int end)
    {
    int i,j;
    if(judge(cur,0,0,0,A,end))
    dfs(cur
    +1,A,end);
    if(end<K)
    {
    A[end]
    =cur;
    end
    ++;
    dfs(cur
    +1,A,end);
    end
    --;
    A[end]
    =-1;
    }
    if(cur-1>res)
    {
    res
    =cur-1;
    memcpy(ans,A,
    sizeof(ans));
    }
    }
    int main()
    {
    int i,j,k,A[15];
    while(1)
    {
    scanf(
    "%d%d",&H,&K);
    if(H==0)
    break;
    memset(A,
    -1,sizeof(A));
    res
    =0;
    dfs(
    1,A,0);
    for(i=0;i<K;i++)
    printf(
    "%3d",ans[i]);
    printf(
    " ->");
    printf(
    "%3d\n",res);
    }
    return 0;
    }

      

  • 相关阅读:
    备忘录模式---行为型
    观察者模式(Observer)---行为型
    Hadoop基础
    centos执行-查看,复制,删除-命令的脚本
    权限问题
    12月centos单词
    配置集群遇到的问题
    SSH--完全分布式主机设置【克隆过安装过Hadoop的主机后】
    java随机排座位
    NewWord
  • 原文地址:https://www.cnblogs.com/staginner/p/2168900.html
Copyright © 2011-2022 走看看