Saint Louis University |
Computer Science 314
|
Dept. of Math & Computer Science |
Topic: Dynamic Programming
Related Reading: Ch. 6
Due:
Friday, 2 May 2008, 1:10pm
Please make sure you adhere to the policies on academic integrity.
See "Solved Exercises" in Chapter 6 of text
For this assignment, we are going to go with two implementation problems. For each you will be asked to submit a working implementation (language of your choice) as well as a brief writeup explaining the high-level structure of your dynamic programming problem. Namely, give an outline of:
Exercise 6 of Chapter 6 (p. 317-318) describes a model for "pretty printing"lines of text given a maximum line width L. It is based on summing penalties computed as the square of the remaining number of spaces left at the end of a line.
You are to implement a solution for that problem, however, with one minor change to the model definition. According to the book, a penalty is assessed for all lines, includiing the last; For this assignment, no penalty should be assessed for the final line (as extra slack there is a reasonable trait).
Write a program named pretty which implements the approach suggested by this problem. Your program should take two command-line arguments, of the form:
pretty filename LIt should break the original file into presumed "words" based on any form of separating whitespace, and then should compute an optimal layout for the text on a line of width L. Formally, your output should include a first line which is the optimal value of the square of the slacks. Following that, you should output the full text based upon the optimal parition, formatting it on a "page" as shown here with width of 55:
23 +-------------------------------------------------------+ |Call me Ishmael. Some years ago, never mind how long | |precisely, having little or no money in my purse, and | |nothing particular to interest me on sure, I thought I | |would sail about a little and see the watery part of | |the world. | +-------------------------------------------------------+
A collection of inputs, together with solutions for various widths can be downloaded here .
Please note that for some of these inputs, there is more than one layout that achieves the optimal value. For example, the above example for ishmael with width 55 could be displayed as
23 +-------------------------------------------------------+ |Call me Ishmael. Some years ago, never mind how long | |precisely, having little or no money in my purse, and | |nothing particular to interest me on sure, I thought | |I would sail about a little and see the watery part of | |the world. | +-------------------------------------------------------+This is the only ambiguous example for the ishmael input, however there are many distinct optimal layouts for all of the declaration or moby examples (in fact, the number of distinct optimal layouts for Moby Dick using 40 colums is 4814945566941474631475780164971994191649746425129036276743010247097935668098220462277520269592003627983710127294136847386370559601812072477043533644651317984501222975183065923977476353181737032626197620544697263331654481371291670310445023443859704266411815441430712402289997175081649767094728871933977191435534832975132394763037361030823936000000000000000000000000)
As a plug for next year's ACM Programming Contest, we borrow a problem modeled after ones that appear in such contests.
The goal of this problem is to determine the minimum amount that must be spent on gas to drive from one city to another, given information about the distances and prices at intermediate gas stations.
Formally, we model the problem as follows.
You will begin at the start city with a full tank of gas, measured in integral units with a tank capacity of T (with T at most 400).
Distances are measured in integral units, such that the final destination is D units from the start (with D at most 100000).
The fuel economy for the car is such that one unit of gas is used to travel one unit of distance.
Before your trip, you are given information regarding intermediate gas stations at which you may purchase gas. There will be S stations (with S at most 100).
For each station, you will be told its distance from the original city as well as the price charged per unit of gas (at most 2000).
File Format: you may assume that the input is given as a text file named gas.txt with the following format. The first line is an integer specifying the overall distance D. The second line is an integer specifying the tank capacity T. The third line is an integer specifying the number of gas stations S. Following that will be S additional lines, one per station ordered in increasing distance from the start. Each station is described with two integers. The first integer is the distance between the original city and the station; the second is the (integral) price charged per unit of gas at this station.
Output Format: Your program should output a single line to stdout, specifying the minimum amount of money that must be spent to get to the goal. If it is not possible to get there (e.g., because two stations are two far apart from each other) you should output the word Impossible.
Resource Usage: The typical rule for these programming contests is that your program be able to complete its task within one minute on a typical PC (i.e., presumably only a few seconds on turing).
Examples: available here. Files are named with the form rand_D_T_S_answer.txt