1998 Fort Lewis College Programming Contest Table of Contents General Instructions Problem 1: Insipid Integers Problem 2: House and Yard Problem 3: Income Tax Problem 4: Water Flow Problem 5: Seminar Registration Problem 6: Pyramidal Prison Problem 7: The Knight and the Princess 2 General Instructions For a solution written in C++, a source file named PROGn.CPP, for a solution written in C, a source file named PROGn.C,. Here n is the assigned number for the problem. The judges will not correct any error, even if it is only a misspelling of the program name, or if the wrong language is specified. Unless otherwise specified in the problem statement, each program will get its input from a file named PROGn.DAT where again n is the assigned number for the problem. The output from each program must go to a file named PROGn.OUT. Your program must read from and write to the current directory – do not specify the disk drive or directory in your program. The first line of output from each program must be: Problem n by Team <team number> where the <team number> is replaced by your team number, and n is replaced by the problem number. The last line of output from every program must be: End of Problem n by Team <team number> There will be no blank lines between the first and last lines of your output, except as specified by the problem statement. Scoring the contest: 1. 2. 3. The team that has solved the most problems in the time allotted is the winner. If two or more teams solve the same number of problems, they are ranked by least total time. The total time is the sum of time consumed for each problem solved. The time consumed for a solved problem is the time elapsed from the beginning of the contest to the submittal of the accepted run plus 20 minutes for each rejected run. There is no time consumed for a problem not solved. Only one correct solution will be accepted for any one problem from a single team. 3 Error Messages There are nine error categories for which a submission may be rejected. They are: SUNTAX ERROR – submitted program will not compile RUN-TIME ERROR – program failed while running. For example, data file name was wrong. TIME-LIMIT EXCEEDED – program took too long to run. Every problem should complete in a reasonable length of time. This means "effectively immediately.” If a program takes more than a few seconds, it is too slow. 4. WRONG ANSWER – self-explanatory. Probably the most common reason for rejection. 5. INACCURATE ANSWER – right answer, wrong accuracy. 6. FAILED TEST CASE – failed at least one of the test cases, passed the others. 7. TOO LITTLE/MUCH OUTPUT – reread the problem. 8. WRONG OUTPUT FORMAT – reread the problem (possibly you forgot to bracket the output with the required team information). 9. RTFI -- you failed to follow all instructions; for example, your source file name is wrong. 10. CHECK CLARIFICATIONS – there has been a clarification of the problem issued by the judges during the contest and you should check the clarification board for guidance. 1. 2. 3. The format of your output is important and must be exactly as shown in the samples, including blank lines and spacing on the line. Failure to follow the required output format will result in a “WRONG OUTPUT FORMAT” error, and your program will be returned for correction. In many cases, the placement and number of blank spaces in the output are as significant as printable characters. Any attempt by your program to write anything to the judge’s disk except the required output files will result in immediate disqualification of your team from the contest. Your team also may be disqualified for any activity that jeopardizes the integrity of the contest such as interfering with other teams or disruptive behavior. Contestants are not to converse with anyone except other members of their team and personnel designated by the contest directors. The contest judges are solely responsible for determining the correctness of submitted solutions. Their decisions are final. 4 Problem 1: Insipid Integers Name your source file Prog1.cpp or Prog1.c A sequence of integers N0 , N1 , N2 , … , is generated using the following algorithm: (1) An initial positive integer N0 is given, (2) The integer Nj+1 is computed as the sum of the squares of the digits of Nj in base 10. It is known that the sequence N0 , N1 , N2 , … will eventually cycle for each choice of N0 . That is, there are integers i and j with i < j where Ni = Nj. If i and j are the smallest integers with 0 <= i < j, and Ni = Nj , then the INSIPID CYCLE LENGTH OF N0 is defined to be j – i and the INSIPID CHAIN LENGTH OF N0 is j . Further, the integer N0 is called INSIPID if the integer 1 occurs in the sequence. NOTE: The insipid cycle length is the number of distinct elements in one cycle and the insipid chain length is the number of distinct elements in the entire sequence. EXAMPLES: (1) If N0 =7, then the sequence proceeds as 7, 49, 97, 130, 10, 1, 1, …. The integer 7 is an insipid integer with cycle length 1 and chain length 6. (2) If N0 = 11112222333311, then the sequence proceeds as 11112222333311, 58, 89, 145, 42, 20, 4, 16, 37, 58, 89, … . The integer 11112222333311 not an insipid integer. The insipid cycle length is 8 and the insipid chain length is 9. Your program is to accept from the input file PROG1.DAT, a positive integer of at most 50 digits in length, determine and output to the file PROG1.OUT whether the input integer is an insipid integer, the insipid cycle length, and the insipid chain length. The following is a listing of the output file PROG1.OUT for Example 1 above (The input file PROG1.DAT contains the single integer 7): Problem 1 by <team name> The integer 7 is an insipid integer The insipid cycle length is 1 The insipid chain length is 6 End of Problem 1 by <team name> The following is a listing of the output file PROG1.OUT for Example 2 above (The input file PROG1.DAT for this example contains the single integer 11112222333311) Problem 1 by <team name> The integer 11112222333311 is not an insipid integer The insipid cycle length is 8 The insipid chain length is 9 End of Problem 1 by <team name> You may assume that the input data file PROG1.DAT contains a single line containing an integer of at most 50 digits beginning in column 1. 5 Problem 2: House and Yard Name your source file Prog2.cpp or Prog2.c A "house" and a "yard" are depicted graphically as rectangles in a two-dimensional Cartesian coordinate system. The sides of the house and the yard are parallel to the coordinate axes and the house is to be contained within the rectangular area of the yard. You are given the coordinates of four points in the plane. Two of these points are supposed to be the endpoints of a diagonal of the rectangle representing the yard and the other two points are supposed to be the endpoints of a diagonal of the rectangle representing the house. The points may be given in any order. Your program is to determine the following: (1) Is it possible that the four points determine a rectangular "yard" containing a rectangular "house"? (2) If (1) above is true, calculate the area of the house and the area of the yard. NOTE: The house and yard may share all or part of one or more edges without the house being outside of the yard. EXAMPLES: (1) Given the four points (3, 10), (9, 8), (7, 5), and (12, 4), the only "house/yard" combination that is possible is: The endpoints of the yard diagonal are: (3, 10) and (12, 4). The endpoints of the house diagonal are: (7, 5) and (9, 8). The area of the house is 6 and the area of the yard is 54. (2) Given the four points (1, 2), (12, 15), (5, 7), and (14, 9), no "house/yard" combination can be formed from these points. A carefully drawn graph reveals that any choice for a diagonal for the yard gives a house which lies partly or wholly outside the yard. Your program is to enter the four points from the input file PROG2.DAT. This file will contain one line of eight integer values (four pairs of coordinates). The contents of the input file for example 1 above is 3 10 9 8 7 5 12 4 The program output is to the file PROG2.OUT. The following is a listing of the output file for Example 1 above: Problem 2 by <team name> The input diagonal endpoints are (3,10), (9,8), (7,5), and (12,4) The endpoints of the yard diagonal are: (3,10) and (12,4) The endpoints of the house diagonal are: (7,5) and (9,8) The area of the yard is: 54 The area of the house is: 6 End of Problem 2 by <team name> The following is a listing of the output file PROG2.OUT for Example 2 above: Problem 2 by <team name> The input diagonal endpoints are (1,2), (12,15), (5,7), and (14,9) No house/yard combination is possible for this data. End of Problem 2 by <team name> 6 Problem 3: Income Tax Computations (2 pages) Name your source file Prog3.cpp or Prog3.c With April 15 approaching, a program is needed to check the computations on Form 1040 for an Individual Income Tax Return. The program is to retrieve data from a data file and create the formatted report shown in the example below. The input data file called PROG3.DAT, consists of one line for each of the following fields. Currency amounts are specified as dollars and cents using a decimal point and do not include commas. Name Social Security Number Number of Dependents Wages, etc. Interest Dividends Other Income Adjustments Federal Income Tax Withheld Estimated Tax Payments A maximum of 30 characters 9 characters Integer Currency Currency Currency Currency Currency Currency Currency Here is an example of such an input file PROG3.DAT: Grace Murray Hopper 324789113 1 43171.68 1375.79 6447.07 0.00 0.00 8416.94 700.00 The output report file for the above input file is to be saved as PROG3.OUT. for the above input data file follows: A listing of the output file Problem 3 by <team name> Tax Computation for: Grace Murray Hopper, 324-78-9113 Wages, etc. 7 43,171.68 Interest 8a 1,375.79 Dividends 9 6,447.07 Other Income 21 0.00 Total Income 22 50,994.54 Adjustments 30 0.00 Adjusted Gross Income 32 50,994.54 Deductions 34 4,000.00 Exemptions 36 2,550.00 Taxable Income 37 44,444.54 Tax 38 9,324.47 Federal Income Tax Withheld 52 8,416.94 Estimated Tax Payments 53 700.00 Total Payments 58 9,116.94 Refund 60a 0.00 Owed 62 207.53 End of Problem 3 by <team name> Your output report is to be formatted exactly as above with all spacings, commas, and decimals as shown. The second field in the report contains the line numbers on Form 1040 and must be included. These line numbers will be used to describe the source data for each entry: 7 The name and social security number are on lines 1 and 2 of the input data file. The social security number as output requires proper formatting. Line 7: direct input. Line 8a: direct input. Line 9: direct input. Line 21: direct input Line 22: add lines 7, 8a, 9, and 21. Line 30: direct input Line 32: subtract line 30 from line 22. Line 34: the standard deduction of $4,000.00 is to be used. Line 36: multiply the number of dependents (direct input) by $2,550.00. Line 37: subtract line 34 and line 36 from line 32 - if the resulting taxable income is negative, enter zero. Line 38: the tax is computed from the TAX RATE SCHEDULE listed below. Line 52: direct input. Line 53: direct input. Line 58: add line 52 and line 53. Line 60a: if line 58 is greater than line 38, enter the difference between line 58 and line 38; otherwise, enter zero. Line 62: if line 38 is greater than line 58, enter the difference between line 38 and line 58; otherwise enter zero. TAX RATE SCHEDULE: If the amount on Enter on Form 1040, line 37, Form 1040, is: But not line 38 of the amount Over-Over-Over-____________________________________________________________________________ $0 24,000 58,150 121,300 263,750 $24,000 58,150 121,300 263,750 ------- $3,600.00 13,162.00 32,738.50 84,020.50 15% + 28% + 31% + 36% + 39.6% An output template for formatting your output file PROG3.OUT follows: 12345678901234567890123456789012345678901234567890 Wages, etc. 7 43,171.68 Interest 8a 1,375.79 Dividends 9 6,447.07 Other Income 21 0.00 $0 24,000 58,150 121,300 263,750 8 Problem 4 : Water Flow Name your source file Prog4.cpp or Prog4.c A city engineer needs a program to analyze the city's water system. Whenever two water pipes join, they form a junction. The junctions are numbered from 1 to 100. Water can flow between two junctions if they are directly connected to each other. In general, water flows from junction I to junction J if a path of direct connections exists between junction I and junction J. The direct connections between junctions are to be read from an ASCII text file named PROG4.DAT. The first line of the file contains the number of junctions to be processed (N). The next N lines of the file contain two integers (I and J) representing a one-way connection from junction I to junction J. The next several lines of the file contain two integers (X and Y) which represent two junction numbers. For each X, Y input pair (an indeterminate number of input lines) the program is to determine if water can flow between junction X and junction Y and is to output one of the two messages shown below: If a path exists beginning at junction X and ending at junction Y, the program is to output the message: WATER CAN FLOW BETWEEN JUNCTIONS X AND Y If no path exists beginning at junction X and ending at junction Y, the following message is produced: WATER CANNOT FLOW BETWEEN JUNCTIONS X AND Y You are provided with the test file PROG4.DAT containing the following connections between junctions: 3 1 5 2 4 6 In the example water system depicted above, water can flow from junction 1 to junction 6, but cannot flow from junction 6 to junction 1. Water can also flow from junction 3 to junction 6, but cannot flow from junction 3 to junction 5. Your output is to be sent to an ASCII text file named PROG4.OUT. The first and last lines of the output file must contain the required team and problem identification, i.e., Problem 4 by <team name> // on the first line End of Problem 4 by <team name> // on the last line 9 Problem 5 : Seminar Registration (2 pages) Name your source file Prog5.cpp or Prog5.c A program is needed to check attendance at a computer seminar. Only registered students are allowed to attend the seminar. A list of registered students is available in an ASCII text file named ROSTER.DAT. A second input file, named ENTRY.DAT contains the names of people who have attempted to enter the seminar room to attend the seminar. The maximum number of students registered for the seminar is 25. Any number of people may attempt to enter the seminar room, but the maximum number of entries in the ENTRY.DAT file will not exceed 100. The program will generate three reports into three separate ASCII text files (all file formats are described below): 1. A report showing how many students were registered for the seminar, how many were present at the meeting, and how many people who tried to enter were denied permission to attend because they had not pre-registered (in a file named HOWMANY.OUT). 2. A report with the list of registered students (sorted by name), with either Present or Absent following each name (in a file named ATTEND.OUT). 3. A report of all people who tried to enter the seminar, but were not allowed to attend because they had not pre-registered (in a file named DENIED.OUT). Input / Output Format Specifications: 1. Each line in the input ROSTER.DAT and ENTRY.DAT files contain a first name (maximum of 10 characters) followed by a blank space, followed by a last name (maximum of 20 characters) terminated by a newline character. Sample ROSTER.DAT and ENTRY.DAT files are provided for testing purposes. 2. The HOWMANY.OUT output file : Summary Attendance Data for Computer Seminar: Number of Registered Students : X Number in Attendance : Y Number Denied Entry : Z 3. The ATTEND.OUT output file (sorted by LastName, then by FirstName) : Attendance Roster for Computer Seminar: LastName FirstName Present (or Absent) LastName FirstName Present (or Absent) . . . Number in Attendance : X Number Absent : Y 10 Problem 5 : Seminar Registration (Cont'd) 4. The DENIED.OUT output file: Denied Attendance for Computer Seminar: LastName FirstName LastName FirstName . . . Number Denied : X 5. The first and last lines of all output files must contain the required team and problem identification, i.e., Problem 5 by <team name> // on the first line End of Problem 5 by <team name> // on the last line 11 Problem 6: Pyramidal Prison (2 pages) Name your source file Prog6.cpp or Prog6.c A prison building has the shape of a pyramid. The number of levels (stories) is NumLevels (which will be part of the input to your program), and the levels are numbered 0..NumLevels-1. The prison cells on each level (except the topmost level) of the building are arranged in an array of concentric squares. The squares are numbered consecutively from inner to outer, with 0 being the innermost square. For example, with NumLevels = 4, the building has four levels numbered 0..3; the first two levels (levels 0 and 1) have the respective floorplans: 9 8 7 6 5 4 3 10 6 5 4 3 2 2 6 5 4 3 2 11 7 3 2 1 1 1 7 3 2 1 1 12 8 4 0 0 0 0 8 4 0 0 0 13 9 5 6 7 15 23 9 5 6 7 15 14 10 11 12 13 14 22 10 11 12 13 14 15 16 17 18 19 20 21 The cell at the center of each level is numbered 0. The successive concentric squares contain 8, 16, 24, … cells. Within each concentric square, the cell directly East of the center is numbered 0; successive cells are numbered in counterclockwise order. Level 0 has (in addition to the cell at the center) NumLevels-1 concentric squares of cells. Each level (other than level 0) has one fewer concentric squares than the level below it. The topmost level has only one cell, numbered 0 and located at the apex of the pyramid. Each night, starting precisely at midnight (12 A.M.), the prison guard (her name is Gerda) begins her inspection tour of the prison. She will need the same amount of time to inspect each cell. She will inspect each level in the prison, starting at level 0, then proceeding to levels 1, 2, … . On each level, she will inspect the squares in order, starting at square 0. Within each square, she will inspect the cells in order, starting at cell 0. Here is an example of an input file Prog6.dat: 4 4 4 4 5 8 1 5 3 3 3 1 12 6 4 4 9 10 48 22 11 12 27 59 12 Each line of the input file contains four numbers: the number of levels of the pyramid, the number of minutes needed to inspect each cell (including the time needed to move to the next cell), and a clock reading in hours and minutes. (All times are A.M.) Your program will determine the guard’s location (which level, which concentric square within that level, and which cell number within that concentric square) at the given time. If the guard has already inspected all cells at the specified time, the time at which the inspection was finished will be printed out by your program. Here is a listing of the output file Prog6.out, resulting from the input listed above. Problem 6 by <team name> Number Minutes Clock Guard's Location Of Per ====================== Levels Cell Level Square Cell ================================================================= 4 1 12:48 A.M. 0 3 23 4 5 6:22 A.M. 2 1 1 4 3 4:11 A.M. 3 0 0 4 3 4:12 A.M. INSPECTION FINISHED AT 4:12 A.M. 5 3 9:27 A.M. INSPECTION FINISHED AT 8:15 A.M. 8 1 10:59 A.M. 5 2 5 End of Problem 6 by <team name> Here is also a formatting template with a few selected lines of the run: 1 2 3 4 5 6 12345678901234567890123456789012345678901234567890123456789012345 Number Minutes Clock Guard's Location Of Per ====================== Levels Cell Level Square Cell ================================================================= 4 1 12:48 A.M. 0 3 23 4 3 4:12 A.M. INSPECTION FINISHED AT 4:12 A.M. 13 Problem 7: The Knight and the Princess (3 pages) Name your source file Prog7.cpp or Prog7.c A cube-shaped building has three levels, numbered 0, 1,2. Each level is square-shaped and consists of nine equal-sized, square-shaped rooms. The location of a room in the building is specified by three numbers: the first two specify the location of the room within a particular level, whereas the third number specifies the level. Level 0 has the following floor plan: North East (0,0,0) (0,1,0) (0,2,0) (1,0,0) (1,1,0) (1,2,0) (2,0,0) (2,1,0) (2,2,0) Levels 1 and 2 have the same floor plan and numbering scheme as level 0. Thus (0,2,1) specifies the room in the Northeast corner of Level 1 and (0,2,2) specifies the room in the Northeast corner of Level 2. There is a door between every pair of adjacent rooms (which are North/South or East/West neighbors of each other). There is also a door in the ceiling / floor of every room allowing passage between every room and its neighbor directly above / below. The legendary captive princess is located in room (2,2,2), and the legendary armored knight is located initially (on Day 0) in room (0,0,0). In each of the remaining rooms resides an evil dragon. The knight is given (by Aphrodite, the Greek goddess of love ) a set of instructions for passing through each room (and slaying the dragon therein) exactly once, until he arrives at the room in which the captive princess resides. Here are the instructions given to the knight by Aphrodite: On Day 0, as already stated, the knight will start in room (0,0,0). On Day 1, the knight will enter room (1,0,0) and slay the dragon therein. On successive days the knight will move according to a code that is provided: "S" means to go STRAIGHT, the next day's move will continue in a straight line in the same direction as in the preceding move. "T" means to TURN, i.e., the next day's move is made by turning 90 degrees from the direction of the preceding move. The turn could be taken in four different directions, but the knight cannot leave the building or visit a room already visited. Proceeding toward his goal at the rate of one room per day, the knight will reach the princess on Day 26. The input file Prog7.dat contains code the knight must follow. It contains one line which is a string of 25 letters each of which is an "S" or a "T", specifying the knight's moves on days 2 through 26. The output file Prog7.out contains a description of all possible paths the knight may take to reach the princess, in the format illustrated below. Here are listings of typical input and corresponding output. In this example, there are two possible paths the knight may take. The input in general will always yield at least one such path. If there are several (more than one) paths, they may be listed in your output in any order. Prog7.dat: STTTSTTSTTTSTSTTTTSTSTSTS 14 Prog7.out: Problem 7 by <team name> ============================ Day Location Command 0 0 0 0 START 1 1 0 0 GO STRAIGHT 2 2 0 0 TURN 3 2 0 1 TURN 4 1 0 1 TURN 5 1 1 1 GO STRAIGHT 6 1 2 1 TURN 7 1 2 2 TURN 8 1 1 2 GO STRAIGHT 9 1 0 2 TURN 10 2 0 2 TURN 11 2 1 2 TURN 12 2 1 1 GO STRAIGHT 13 2 1 0 TURN 14 1 1 0 GO STRAIGHT 15 0 1 0 TURN 16 0 1 1 TURN 17 0 0 1 TURN 18 0 0 2 TURN 19 0 1 2 GO STRAIGHT 20 0 2 2 TURN 21 0 2 1 GO STRAIGHT 22 0 2 0 TURN 23 1 2 0 GO STRAIGHT 24 2 2 0 TURN 25 2 2 1 GO STRAIGHT 26 2 2 2 FINISH ============================ Day Location Command 0 0 0 0 START 1 1 0 0 GO STRAIGHT 2 2 0 0 TURN 3 2 1 0 TURN 4 1 1 0 TURN 5 1 1 1 GO STRAIGHT 6 1 1 2 TURN 7 1 2 2 TURN 8 1 2 1 GO STRAIGHT 9 1 2 0 TURN 10 2 2 0 TURN 11 2 2 1 TURN 12 2 1 1 GO STRAIGHT 13 2 0 1 TURN 14 1 0 1 GO STRAIGHT 15 0 0 1 TURN 16 0 1 1 TURN 17 0 1 0 TURN 18 0 2 0 TURN 19 0 2 1 GO STRAIGHT 20 0 2 2 TURN 21 0 1 2 GO STRAIGHT 22 0 0 2 TURN 23 1 0 2 GO STRAIGHT 24 2 0 2 TURN 25 2 1 2 GO STRAIGHT 26 2 2 2 FINISH End of Problem 7 by <team name> 15 The first command ('S' -- "STRAIGHT") in the above input file tells the knight that the rooms which he passes through on Days 0, 1, and 2 lie on a straight line. Given his locations on Days 0 and 1, this command tells the knight his location on Day 2 without ambiguity. Similarly, the "STRAIGHT" command on the "Day 5" line of the output means that the knight's locations on Days 4, 5, and 6 lie on a straight line. On the first of the two paths listed in the output, this meant a West-to-East horizontal straight line; on the second path this meant a vertical straight line, from level 0 to level 2. Each "STRAIGHT" or "TURN" command listed for a particular day of the output tells the knight how to reach his location on the following day. The command 'T' -- "TURN" merely tells the knight to take a 90-degree turn, but does not tell him in which direction to turn. Thus the first 'T' (the second letter) in the input file tells the knight that his path will take a 90-degree turn at the room which he entered on Day 2. On the first of the two paths listed in the output, this put the knight into the room at location (2,0,1) on Day 3, on the second path, at location (2,1,0). Under the "Command" header of each possible path in the output, the command for Day 0 is always "START" and for Day 26 it is always "FINISH". For Days 1 through 25 of the output, the commands are those whose abbreviations were read from the input file; they represent the moves taken by the knight on days 2 through 26. Finally, here is a template for formatting your output: 123456789012345678901234567890 Day Location Command 0 0 0 0 START 1 1 0 0 GO STRAIGHT