Sha 165 Stamps

Source: Internet
Author: User

UVA_165

Because h + k <= 9, the data volume is not large, so you can directly enumerate and perform deep search.

There are two deep search functions, one of which is used to enumerate the values that can be continuously achieved. If you can spell out the current value through the existing face value, you can continue to maintain the status of the existing face value and continue the deep search, alternatively, you can create a new value equal to the current value and perform further search. when neither of the two methods can spell out the current value, the current value minus 1 is the maximum value that can be spelled out under the current face value. Another deep search function is used to determine whether the current value can be spelled out through the existing face value, you only need to select whether to select the stamp with the current face value in sequence and make the total number of stamps smaller than h. When the current face value can be spelled out at a certain time point, 1 is returned, if the current face value cannot be spelled out in all cases, 0 is returned.

#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;
}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.