UVA 165 Stamps (backtracking)

Source: Internet
Author: User

UVA 165 Stamps



The government of Nova Mareterrania requires that various legal documents has stamps attached to them so, the GOVERNM Ent can derive revenue from them. In terms of recent legislation, each class of document was limited in the number of stamps that may was attached to it. The Government wishes to know about many different stamps, and of what values, they need to print to allow the widest choice of values to is made up under these conditions. Stamps is always valued in units of $.

This had been analysed by government mathematicians who had derived a formula for N ( h , K ), WH Ere H is the number of stamps. Attached to a document, and K is the number of denominations of STA MPs available, and N is the largest attainable value of a continuous sequence starting from $. For instance, if h =3, K =2 and the denominations is $ and $4, we can make all the values from $ to $6 ( As well as $8, $9 and $). However with the same values of H and K , but using $ and $ stamps we can make all the values from $ to $7 (as well as $9). This is maximal, so n (3,2) = 7.

Unfortunately the formula relating n(h,K) to H,K and the values of the stamps Has been lost--it is published in one of the government reports but No-one can remember which one, and of the three Rese Archers started to search for the formula, and the boredom third a job as a took lighthouse keeper It provided more social stimulation.

The task has now been passed in to you. You doubt the existence of a formula in the first place so you decide to write a program that, for given values ofh and K, would determine an optimum set of stamps and the value ofn(h,K).

Input

Input would consist of several lines, each containing a value for H andK. The file is being terminated by zeroes (0 0). For technical reasons the sum ofH and K are limited to 9. (The president lost his little finger in a shooting accident and cannot count past 9).

Output

Output would consist of a line for each value of H and K consisting of theK stamp values in ASCE Nding order right justified in fields 3 characters wide, followed by a space and a arrow ( -> ) and the value ofn (h,K) Right justified in a field 3 characters wide.

Sample Input
3 20 0
Sample Output
  1  3-  7



The main topic: there are two data for each sample group: H and K (0,0) as the end of the sample. H is the maximum amount of stamps that can be affixed, and K is the type of denomination for which the stamps are given. The longest sequential sequence (from the beginning) that is required to be composed of K-type 1~h stamps.

problem-Solving ideas : Two-story backtracking, the first layer to find K stamps different denominations of the way, the second layer to find the current collocation mode can be composed of the longest continuous sequence.


#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm>using namespace Std;int h, K, ans, temp, num[200], num2[200], vis[200], flag;void DFS2 (int sum, int result, int step) {//check whether the current denomination can compose res ULTIF (flag) return;if (sum = = result) {flag = 1;return;} if (step = h) return;for (int i = 0; i < K; i++) {sum + = Num[i];D FS2 (sum, result, step + 1); sum-= Num[i];}} void DFS (int step) {if (step = k) {temp = H;do {///find the longest continuous sequence that can consist of the current denomination flag = 0;temp++;D FS2 (0, temp, 0 );} while (flag), if (Temp-1 > ans) {ans = temp-1;for (int i = 0; i < K; i++) {num2[i] = Num[i];}} return;} for (int i = num[step-1]; I <= temp + 7; i++) {//backtrack to find different denomination combinations if (!vis[i] && i > num[step-1]) {Vis[i] = 1;num[step] = I;dfs (step + 1); Vis[i] = 0;}}} int main () {while (scanf ("%d%d", &h, &k) = = 2, h | | k) {ans = 0;memset (Vis, 0, sizeof (VIS)); memset (num, 0, sizeof (num)); memset (num2, 0, sizeof (num2)); flag = 0;num[0] = 1;vis[1] = 1;tEMP = H;dfs (1); for (int i = 0; i < K; i++) {//Note output format printf ("%3d", Num2[i]);} printf ("->%3d\n", ans);} return 0;}



UVA 165 Stamps (backtracking)

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.