|
| 1 | +/* |
| 2 | + * Packing knapsack question |
| 3 | + * |
| 4 | + * Copyright (C) 2016, Taeung Song <[email protected]> |
| 5 | + * |
| 6 | + */ |
| 7 | +#include <stdio.h> |
| 8 | +#include <stdbool.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <string.h> |
| 11 | + |
| 12 | +#define MAX_INPUT 16 |
| 13 | + |
| 14 | +struct knapsack { |
| 15 | + unsigned int maxprice; |
| 16 | +} *knapsack_list; |
| 17 | + |
| 18 | +struct jewelry { |
| 19 | + unsigned int wgt; |
| 20 | + unsigned int price; |
| 21 | +}; |
| 22 | + |
| 23 | +unsigned int limited_wgt; |
| 24 | + |
| 25 | +unsigned int get_cond_maxprice(int wgt, struct jewelry *jewelry) |
| 26 | +{ |
| 27 | + /* Get maximum price based on a specific weight |
| 28 | + * following a specific jewelry. |
| 29 | + */ |
| 30 | + int i; |
| 31 | + int rest_wgt = wgt - jewelry->wgt; |
| 32 | + int price = jewelry->price + knapsack_list[rest_wgt].maxprice; |
| 33 | + |
| 34 | + return knapsack_list[wgt].maxprice < price ? |
| 35 | + price : knapsack_list[wgt].maxprice; |
| 36 | +} |
| 37 | + |
| 38 | +void pack_knapsack(struct jewelry *jewelry) |
| 39 | +{ |
| 40 | + /* Case by case pack knapsack following maximum |
| 41 | + * price per limited weight. |
| 42 | + */ |
| 43 | + int wgt; |
| 44 | + |
| 45 | + if (limited_wgt < jewelry->wgt) |
| 46 | + return; |
| 47 | + |
| 48 | + for (wgt = 0; wgt <= limited_wgt; wgt++) { |
| 49 | + if (jewelry->wgt <= wgt) { |
| 50 | + unsigned int maxprice = get_cond_maxprice(wgt, jewelry); |
| 51 | + |
| 52 | + if (knapsack_list[wgt].maxprice < maxprice) |
| 53 | + knapsack_list[wgt].maxprice = maxprice; |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +bool get_values_from(char *input, unsigned int *val1, unsigned int *val2) |
| 59 | +{ |
| 60 | + char *arg; |
| 61 | + char *ptr = strdup(input); |
| 62 | + |
| 63 | + if (!ptr) { |
| 64 | + printf("%s: strdup failed\n", __func__); |
| 65 | + exit(1); |
| 66 | + } |
| 67 | + |
| 68 | + arg = strsep(&ptr, " "); |
| 69 | + *val1 = atoi(arg); |
| 70 | + if (ptr == NULL) { |
| 71 | + printf("Error: Need a whitespace\n"); |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + *val2 = atoi(ptr); |
| 76 | + |
| 77 | + return true; |
| 78 | +} |
| 79 | + |
| 80 | +int main(int argc, const char **argv) |
| 81 | +{ |
| 82 | + int i; |
| 83 | + struct jewelry *jewels; |
| 84 | + char input[MAX_INPUT]; |
| 85 | + unsigned int nr_jewels; |
| 86 | + |
| 87 | + fgets(input, sizeof(input), stdin); |
| 88 | + if (get_values_from(input, &nr_jewels, &limited_wgt) == false) |
| 89 | + return -1; |
| 90 | + |
| 91 | + jewels = malloc(sizeof(struct jewelry) * nr_jewels); |
| 92 | + for (i = 0; i < nr_jewels; i++) { |
| 93 | + bool ret; |
| 94 | + |
| 95 | + fgets(input, sizeof(input), stdin); |
| 96 | + ret = get_values_from(input, &jewels[i].wgt, |
| 97 | + &jewels[i].price); |
| 98 | + if (ret == false) |
| 99 | + return -1; |
| 100 | + } |
| 101 | + |
| 102 | + /* from 0 to last limited weight */ |
| 103 | + knapsack_list = malloc(sizeof(struct knapsack) * (limited_wgt + 1)); |
| 104 | + |
| 105 | + for (i = 0; i <= limited_wgt; i++) |
| 106 | + knapsack_list[i].maxprice = 0; |
| 107 | + |
| 108 | + for (i = 0; i < nr_jewels; i++) |
| 109 | + pack_knapsack(&jewels[i]); |
| 110 | + |
| 111 | + printf("%d\n", knapsack_list[limited_wgt].maxprice); |
| 112 | + free(jewels); |
| 113 | + free(knapsack_list); |
| 114 | + return 0; |
| 115 | +} |
0 commit comments