186 lines
		
	
	
		
			6.0 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			186 lines
		
	
	
		
			6.0 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
| ---
 | |
| id: 5a23c84252665b21eecc7ed4
 | |
| title: ナップサック問題 / 個数制限なし
 | |
| challengeType: 5
 | |
| forumTopicId: 323655
 | |
| dashedName: knapsack-problemunbounded
 | |
| ---
 | |
| 
 | |
| # --description--
 | |
| 
 | |
| ある旅人が道から外れ、予定外の場所に立ち寄ることを余儀なくされたところ、そこは理想郷だったのです。 その地を離れるにあたり、そこで入手できるアイテムを好きなだけ、ナップサックに入れて運べる限り持って行くことを許されています。
 | |
| 
 | |
| 旅人は、合計で最大重量を超えるものを運ぶことができないことを知っていますし、ナップサックの容量は限られています。
 | |
| 
 | |
| アイテムのバーコードのすぐ上を見ると、重量と容量がわかるようになっています。 旅人は持っていた最近の経済紙を探し出して、それぞれのアイテムの価値を把握しました。
 | |
| 
 | |
| どのアイテムもひとまとまり全体を取る必要がありますが、それぞれ、これまで運ぶことのできた量をはるかに超える数量があります。
 | |
| 
 | |
| # --instructions--
 | |
| 
 | |
| オブジェクトの配列、最大重量、最大容量をパラメータとして取る関数を記述してください。 各オブジェクトには アイテム名、価値、重量、容量の 4 つの属性があります。 この関数は、旅人が持っていくことのできるアイテムの価値の最大値を返さなければなりません。
 | |
| 
 | |
| # --hints--
 | |
| 
 | |
| `knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 25, 0.25)` は `54500` を返す必要があります。
 | |
| 
 | |
| ```js
 | |
| assert.equal(
 | |
|   knapsackUnbounded(
 | |
|     [
 | |
|       { name: 'panacea', value: 3000, weight: 0.3, volume: 0.025 },
 | |
|       { name: 'ichor', value: 1800, weight: 0.2, volume: 0.015 },
 | |
|       { name: 'gold', value: 2500, weight: 2, volume: 0.002 }
 | |
|     ],
 | |
|     25,
 | |
|     0.25
 | |
|   ),
 | |
|   54500
 | |
| );
 | |
| ```
 | |
| 
 | |
| `knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 55, 0.25)` は `88400` を返す必要があります。
 | |
| 
 | |
| ```js
 | |
| assert.equal(
 | |
|   knapsackUnbounded(
 | |
|     [
 | |
|       { name: 'panacea', value: 3000, weight: 0.3, volume: 0.025 },
 | |
|       { name: 'ichor', value: 1800, weight: 0.2, volume: 0.015 },
 | |
|       { name: 'gold', value: 2500, weight: 2, volume: 0.002 }
 | |
|     ],
 | |
|     55,
 | |
|     0.25
 | |
|   ),
 | |
|   88400
 | |
| );
 | |
| ```
 | |
| 
 | |
| `knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 25, 0.15)` は`42500` を返す必要があります。
 | |
| 
 | |
| ```js
 | |
| assert.equal(
 | |
|   knapsackUnbounded(
 | |
|     [
 | |
|       { name: 'panacea', value: 3000, weight: 0.3, volume: 0.025 },
 | |
|       { name: 'ichor', value: 1800, weight: 0.2, volume: 0.015 },
 | |
|       { name: 'gold', value: 2500, weight: 2, volume: 0.002 }
 | |
|     ],
 | |
|     25,
 | |
|     0.15
 | |
|   ),
 | |
|   42500
 | |
| );
 | |
| ```
 | |
| 
 | |
| `knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 35, 0.35)` は`75900` を返す必要があります。
 | |
| 
 | |
| ```js
 | |
| assert.equal(
 | |
|   knapsackUnbounded(
 | |
|     [
 | |
|       { name: 'panacea', value: 3000, weight: 0.3, volume: 0.025 },
 | |
|       { name: 'ichor', value: 1800, weight: 0.2, volume: 0.015 },
 | |
|       { name: 'gold', value: 2500, weight: 2, volume: 0.002 }
 | |
|     ],
 | |
|     35,
 | |
|     0.35
 | |
|   ),
 | |
|   75900
 | |
| );
 | |
| ```
 | |
| 
 | |
| `knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 15, 0.25)` は `43200` を返す必要があります。
 | |
| 
 | |
| ```js
 | |
| assert.equal(
 | |
|   knapsackUnbounded(
 | |
|     [
 | |
|       { name: 'panacea', value: 3000, weight: 0.3, volume: 0.025 },
 | |
|       { name: 'ichor', value: 1800, weight: 0.2, volume: 0.015 },
 | |
|       { name: 'gold', value: 2500, weight: 2, volume: 0.002 }
 | |
|     ],
 | |
|     15,
 | |
|     0.25
 | |
|   ),
 | |
|   43200
 | |
| );
 | |
| ```
 | |
| 
 | |
| # --seed--
 | |
| 
 | |
| ## --seed-contents--
 | |
| 
 | |
| ```js
 | |
| function knapsackUnbounded(items, maxweight, maxvolume) {
 | |
| 
 | |
| }
 | |
| ```
 | |
| 
 | |
| # --solutions--
 | |
| 
 | |
| ```js
 | |
| function knapsackUnbounded(items, maxWeight, maxVolume) {
 | |
|   function getPickTotals(items, pick) {
 | |
|     let totalValue = 0;
 | |
|     let totalWeight = 0;
 | |
|     let totalVolume = 0;
 | |
|     for (let i = 0; i < items.length; i++) {
 | |
|       totalValue += pick[i] * items[i].value;
 | |
|       totalWeight += pick[i] * items[i].weight;
 | |
|       totalVolume += pick[i] * items[i].volume;
 | |
|     }
 | |
|     return [totalValue, totalWeight, totalVolume];
 | |
|   }
 | |
| 
 | |
|   function getMaxes(items, maxWeight, maxVolume) {
 | |
|     const maxes = [];
 | |
|     for (let i = 0; i < items.length; i++) {
 | |
|       const maxUnitsInWeight = Math.floor(maxWeight / items[i].weight);
 | |
|       const maxUnitsInVolume = Math.floor(maxVolume / items[i].volume);
 | |
|       const maxUnitsInLimit = Math.min(maxUnitsInWeight, maxUnitsInVolume);
 | |
|       maxes.push(maxUnitsInLimit);
 | |
|     }
 | |
|     return maxes;
 | |
|   }
 | |
| 
 | |
|   function isInLimit(value, limit) {
 | |
|     return value <= limit;
 | |
|   }
 | |
| 
 | |
|   function getCombinations(maxValues, curPicks, combinations) {
 | |
|     if (maxValues.length === 0) {
 | |
|       combinations.push(curPicks);
 | |
|     }
 | |
| 
 | |
|     const curMax = maxValues[0];
 | |
|     const leftMaxValues = maxValues.slice(1);
 | |
|     for (let i = 0; i <= curMax; i++) {
 | |
|       getCombinations(leftMaxValues, curPicks.concat(i), combinations);
 | |
|     }
 | |
|     return combinations;
 | |
|   }
 | |
| 
 | |
|   let bestValue = 0;
 | |
|   let bestPick = [];
 | |
|   const maxes = getMaxes(items, maxWeight, maxVolume);
 | |
|   const combinations = getCombinations(maxes, [], []);
 | |
|   for (let i = 0; i < combinations.length; i++) {
 | |
|     const curPick = combinations[i];
 | |
|     const [curValue, curWeight, curVolume] = getPickTotals(items, curPick);
 | |
|     if (!isInLimit(curWeight, maxWeight) || !isInLimit(curVolume, maxVolume)) {
 | |
|       continue;
 | |
|     }
 | |
| 
 | |
|     if (curValue > bestValue) {
 | |
|       bestValue = curValue;
 | |
|       bestPick = [curPick];
 | |
|     } else if (curValue === bestValue) {
 | |
|       bestPick.push(curPick);
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   return bestValue;
 | |
| }
 | |
| ```
 |