Files
CS412-Applied-Algorithms/Backtracking/README.md

117 lines
4.5 KiB
Markdown

# Coding 3: Backtracking
## Rocket Sections
In order to identify budding rocket science talent, SpaceX has designed a new
modular build-your-own rocket kit for amateur rocketeers to use to construct
model rockets. After careful user-studies, SpaceX has found that its customers
want to be able to build rockets of widely varying height. Thus, they've arrived
at the following idea. Instead of selling rocket fuselage tubing at every height
desired by their customers, they'll offer an array of rocket "sections" at
varying heights that can be fit together to form a full sized rocket. For
example, for the Lil Falcons parts (their entry level kit for preschoolers, that
comes complete with a mix your own rocket fuel kit and size 3T smocks) they
offer rocket sections in the following sizes: 1 inch, 2 inch 5 inch, and 7 inch
sections.
In order to build a rocket of height 15, a kinderscientist could use two 5"
sections, two 2" sections, and one 1" section for a total of five sections. Or
three 5" sections for a total of three sections. Obviously, construction is made
more complicated by the number of sections that need to be arc welded together
(there's a companion Kids-Do-Welding TM starter kit made by Hasbro for the
preschooler who hasn't gotten started welding yet). The goal of any good rocket
designer is to minimize the number of sections used in the construction of their
rocket.
## Input
The input to the program will be a line containing a space separated, sorted
list of the n lengths of available rocket sections for a given kit. The second
line will be a single integer representing the length of rocket the user wants
to construct. The first line will always include a part of length 1, meaning
that all requested lengths can be constructed.
## Output
For partial credit you can simply output a single line reporting the minimum
total number of rocket sections needed to construct the rocket in the format "N
rocket sections minimum\n".
For full credit the output should have n+1 lines. The first n lines should
detail how many of each rocket length the designer needs to buy. Each should be
of the form "X of length Y" detailing how many (X) rocket sections of length
value Y should be bought. The final line should report the total number of
rocket sections in the format "N rocket sections minimum".
### Sample Partial Credit Input/Output
<table>
<tr>
<td>Sample Input</td>
<td>Sample Output</td>
</tr>
<tr>
<td><pre>1 2 5 7<br>15</pre></td>
<td><pre>3 rocket sections minimum</pre></td>
</tr>
<tr>
<td><pre>1 2 3 5 7 14<br>15</pre></td>
<td><pre>2 rocket sections minimum</pre></td>
</tr>
<tr>
<td><pre>1 9<br>17</pre></td>
<td><pre>9 rocket sections minimum</pre></td>
</tr>
<tr>
<td><pre>1 9 10<br>18</pre></td>
<td><pre>2 rocket sections minimum</pre></td>
</tr>
<tr>
<td><pre>1 9 10 90 100<br>198</pre></td>
<td><pre>4 rocket sections minimum</pre></td>
</tr>
</table>
### Sample Full Credit Input/Output
<table>
<tr>
<td>Sample Input</td>
<td>Sample Output</td>
</tr>
<tr>
<td><pre>1 2 5 7<br>15</pre></td>
<td><pre>1 of length 1<br>0 of length 2<br>0 of length 5<br>2 of length 7<br>3 rocket sections minimum</pre></td>
</tr>
<tr>
<td><pre>1 2 3 5 7 14<br>15</pre></td>
<td><pre>1 of length 1<br>0 of length 2<br>0 of length 3<br>0 of length 5<br>0 of length 7<br>1 of length 14<br>2 rocket sections minimum</pre></td>
</tr>
<tr>
<td><pre>1 9<br>17</pre></td>
<td><pre>8 of length 1<br>1 of length 9<br>9 rocket sections minimum</pre></td>
</tr>
<tr>
<td><pre>1 9 10<br>18</pre></td>
<td><pre>0 of length 1<br>2 of length 9<br>0 of length 10<br>2 rocket sections minimum</pre></td>
</tr>
<tr>
<td><pre>1 9 10 90 100<br>198</pre></td>
<td><pre>0 of length 1<br>2 of length 9<br>0 of length 10<br>2 of length 90<br>0 of length 100<br>4 rocket sections minimum</pre></td>
</tr>
</table>
## Your Task
Develop a recursive backtracking solution to the problem. Turn your solution in
as `cs412_hw3_a.py` to Gradescope.
> Eclypse's Note: The above is a lie, Gradescope actually wanted the file to be
> named `cs412_rockets.py` as of 20:06 2025-09-13
### Rubric:
- (10 points) Does the code solve the samples in the write-up?
- (7 point) Does the code solve the private testing samples?
- (3 points) For solving the full credit option.