Westminster College 2012 High School Programming Contest October 8, 2012 Rules: 1. There are six questions to be completed in two and 1/2 hours. 2. All questions require you to read the test data from standard input and write results to standard output. You should not use files for input or output. 3. The allowed programming languages are C++ and Java. 4. All programs will be re-compiled prior to testing with the judges’ data. 5. Programming style is not considered in this contest. You are free to code in whatever style you prefer. Documentation is not required. 6. Judges’ decisions are final. No cheating will be tolerated. 2012 Westminster High School Programming Contest Problem A: 1 The 3n + 1 problem The 3n + 1 problem is a very interesting mathematical problem which goes as follows: start with any positive integer n, and then do one of the following: 1. if n is even, replace n with n/2. 2. if n is odd, replace n with 3n + 1. So, if you start with n = 38, you would first replace it with 38/2 = 19. Next you would replace 19 with 3(19) + 1 = 58. This, in turn, would be replaced by 29, which is replaced by 88, and so on. That’s it. So where’s the “very interesting” part you ask? Well, for every starting positive number n that’s ever been tried, this procedure eventually generates 1. Using the example above, the series of numbers generated is 38 → 19 → 58 → 29 → 88 → 44 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 And the REALLY interesting thing about it is that no one has yet been able to prove that this will ALWAYS be the case for any positive integer n (the problem was first posed by L. Collatz in 1937 and remains one of the most famous unanswered questions in mathematics). Now, needless to say, I’m not asking you to prove the 3n + 1 problem, but I am asking you to implement it. Input The input file will consist of multiple test cases. Each case consists of a single positive integer n. The maximum value for n will be 100,000,000 (NOTE: C++ users, you’ll want to use the type long long for n, not int or long; Java users should use the type long). A line containing a single 0 will terminate input. Output For each test case, output the case number followed by the number of times you had to replace the starting number until you get to 1. Sample Input 38 2 1024 0 Sample Output Case 1: 21 Case 2: 1 Case 3: 10 2012 Westminster High School Programming Contest Problem B: 2 I’m Asking You to Check, Mate The game of chess is played on an 8 × 8 board, and each side has 16 pieces consisting of kings, queens, rooks, bishops, knights and pawns (we’ll ignore pawns for this problem). Each piece has a unique way to move on the board as follows: the queen is the most powerful piece, and can move to any square in the same row, column or diagonal which it is currently on, as shown in the first figure below: the queen on square (3,2) can move to squares (3,4), (8,7), (4,2) and a host of others (23 all together). This figure also shows how the king moves. It too can move in any direction, but only one square at a time. The rook can only move in the same row or column it is currently on, and the bishop can only move on a diagonal which is currently on (see figure 2). The strangest piece is the knight, which moves in an L-shaped manner – 2 squares in any direction along its row or column, and then 1 square in a direction at right angles to this (see figure 3 – we will use N for a knight, since K is already used for the king). 8 6 @ I K6 7 ?@ R 6 5 4 @ I 3 @ @ 2 Q ? @ R 1 1 2 3 4 5 6 7 8 Figure 1 8 6 @ I 7 B @ 6 @ 5 @ R @ 4 3 2 R ? 1 1 2 3 4 5 6 7 8 Figure 2 8 7 6 5 4 3 2 1 6 N ? 6 ? - 1 2 3 4 5 6 7 8 Figure 3 If any of your opponent’s pieces are on a square that one of your pieces can move to, it is under attack , meaning that your piece can move to that square and remove the opposing piece from the board. The object of a game of chess is to checkmate the opposing king, which occurs when 1. the opposing king is under attack (i.e., one of you pieces could move to the king’s position), and 2. every possible square the king could move to is also under attack. For example, the opposing king (the circled K) in Fig 4 below is checkmated: it is currently under attack by your queen, and can’t move to any safe square because of your queen, rooks and the knight. The king in Fig 5 is not checkmated, as it can move safely to squares (3, 5) or (3, 6) since none of your pieces are attacking either of those squares. 8 7 6 5 4 3 2 1 Q R Kl N R 1 2 3 4 5 6 7 8 Figure 4 8 7 6 5 4 3 2 1 Q R Kl N 1 2 3 4 5 6 7 8 Figure 5 2012 Westminster High School Programming Contest 3 For this problem, you will read in the positions of several of your pieces and the position of the opposing king and must determine whether or not the opposing king has been checkmated. Input There will be multiple test cases. Each test case will start with a line containing a positive integer n indicating the number of total pieces on the board (n-1 of your pieces and 1 opposing king). Following that will be n lines, each containing a single character following by a pair of integers. The single character identifies the type of piece (either ’K’, ’Q’, ’R’, ’B’ or ’N’) and the pair of integers specifies the column and row location of the piece. The opposing king will always be the last piece specified. Unlike real chess, there can be any number of your kings, queens, rooks, bishops and knights on the board, but there will always be only one opposing king. You may also assume that none of your pieces is next to the opposing king, and that no piece gets in the way of any other piece. A single 0 will terminate input. The two sample inputs below correspond to figures 4 and 5 above. Output For each test case, output the case number followed by either Yes if the king is checkmated, or No m if the king is not checkmated and has m safe squares to move to. Sample Input 5 Q R R N K 4 Q R N K 0 2 3 7 6 4 8 1 7 4 6 2 7 6 4 8 7 4 6 Sample Output Case 1: Yes Case 2: No 2 2012 Westminster High School Programming Contest Problem C: 4 It All Adds Up The number 51 can be written as the sum of consecutive positive integers in two different ways: 51 = 25+26 and 51=16+17+18. Hey wait, there’s a third way! 51 = 6+7+8+9+10+11. Neat! Sounds like an idea for a programming problem! Input The input file will consist of multiple test cases. Each case consists of a single positive integer n. The maximum value for n will be 100,000. A line containing a single 0 will terminate input. Output For each test case, output the case number and an integer m indicating the number of ways which the value of n for that problem can be broken down to a sum of consecutive integers (we’ll call each of these sums a partition). After this, output the m partitions, one per line, in the form a b where a + (a + 1) + (a + 2) + · · · + b is one of the ways to get n. Output the partition with the smallest a value first, then the next smallest, etc. Sample Input 51 8 52 0 Sample Output Case 1: 3 6 11 16 18 25 26 Case 2: 0 Case 3: 1 3 10 2012 Westminster High School Programming Contest Problem D: 5 Scoooooooooooooore! You work for an on-line newspaper called “The Daily Blab” and have one and only one job: to post football scores once the games are over. You have an assistant at each game who tweets every time some team scores. To keep things simple (and within the 140 character limit), your assistant only tweets the first letter of the scoring team’s name, and what they did to score. In (American) football there are five ways to score points: a touchdown, worth 6 points; an extra point, worth 1 point; a two-point conversion worth (what else) 2 points; a field goal, worth 3 points; and a safety, worth 2 points (actually, there’s a sixth way as well – do any of you know what it is?). So if the Steelers are playing the Ravens, a tweet might look like “S T”, meaning the Steelers scored a touchdown, or “R F” meaning the Ravens scored a field goal (no matter how unlikely). An “E”, “C” and an “S” are used to indicate an extra point, a two-point conversion and a safety. After the game is over you can use these tweets to determine the final score, which you then post. For example, if you got the tweets “S T”, “S E”, “R F”, “S F”, “S S”, you would post “Steelers 12, Ravens 3”. Input The input file will consist of multiple games. The input file starts with an integer n indicating the number of games. Each game starts with a line containing the team names, which always start with different letters. Following this will be the tweets, one per line. Each tweet will consist of two characters separated by a space. For all but the last tweet the first character will be one of the two starting characters for the team name, and the second character will be either “T”, “E”, “C”, “F” or “S”. All characters will be uppercase. The last tweet in each game will be “$ $” indicating the game is over. Output For each game output the game number followed by the winning teams name and score, and the losing teams name and score, each separated by a single space. There will never be a tie game. Sample Input 2 Steelers Ravens S T S E R F S F S S $ $ Cowboys Redskins R T R T R T R T $ $ Sample Output Game 1: Steelers 12 Ravens 3 Game 2: Redskins 24 Cowboys 0 2012 Westminster High School Programming Contest Problem E: 6 And Speaking of Tweets You are all well aware of the Twitter phenomenon, and the fact that tweets can only be 140 characters long. You’ve just been promoted to Head Twit at a spinoff company called Twitter Lite which only allows 50 characters per tweet. Your job is to enforce this limit. You read each users tweets and truncate them if they go over the 50 character limit. To make up for their smaller tweet size, Twitter Lite has a somewhat more lenient policy as to what counts as a character, only counting non-whitespace characters towards the total (i.e., they don’t count spaces, tabs or new-line characters). Input The input file will consist of multiple test cases. The first line will contain an integer n indicating the number of test cases. Each test case will be on a single line and will contain only alphanumeric characters, punctuation and spaces. There will never be two or more consecutive spaces in any line. Output For each test case output the first message as written up to and including the 50th character (not counting spaces). If the message is less than 50 characters to start with, just output the message in its entirety. Sample Input 2 This is a short tweet that won’t get truncated. This tweet contains 70 characters so it will get truncated before it gets to the end. Sample Output This is a short tweet that won’t get truncated. This tweet contains 70 characters so it will get truncated b Reading in Lines C++: To read in an entire line into a string variable s use the global function getline() found in the string library. #include <string> using namespace std; ... string s; ... getline(cin,s); Java: To read in an entire line into a String variable s, use the nextLine method of a Scanner object. import java.util.Scanner; ... Scanner in = new Scanner(System.in); String s; ... s = in.nextLine(); 2012 Westminster High School Programming Contest 7 2012 Westminster High School Programming Contest Problem F: 8 You’ve Got Nothing to Hit but the Heights Brock Buster is a big-time Hollywood director who’s looking for three actors for his next film to play the roles of the crotchety yet loveable grandfather, the hard-driven and neglectful father, and the winsome but hard-as-nails son. Brock has a specific image in his head about the relative heights for these roles. He wants the grandfather to be 3 inches taller than the father, who in turn should be 6 inches taller than the son. This is a MUST (along with the fresh papaya-and-sprouts smoothie Brock demands each morning before filming). You’re in charge of the casting call, and you figure the first thing you have to do is to satisfy Brock’s height requirements. You have the portfolios of everyone who wants to audition, and from this you want to pull out all the groups of three actors who have the appropriate heights. This has turned out to be a little harder than you planned, so you decide to write a program to do it for you (Wow! – Hollywood casting agent AND computer programmer – you must be pretty talented!). Input The input file will consist of multiple test cases. Each case starts with an integer n ≤ 200 indicating the number of actors auditioning for the rolls. After this are n lines of the form name height where name is the name of the actor and height is the actor’s height in inches. Each name will be a single word and no two actors will have the same height. A line with a single zero will terminate input. Output For each test case, output the case number on a single line, following by one line for each appropriate group of actors. Within each group, print the tallest person’s name first, followed by the name of the next tallest and then the shortest person’s name. Separate each name with a single blank. Print the “tallest” group first, i.e. the group with the tallest first person, followed by the second tallest group, etc. Follow the format shown below. 2012 Westminster High School Programming Contest Sample Input 8 Ford 61 Depp 69 DiCaprio 63 Hanks 71 Pitt 70 Statham 62 Smith 72 Chan 67 4 Nicholson 70 Willis 64 Crowe 66 McGregor 68 3 Bale 50 Stallone 56 Freeman 59 0 Sample Output Case 1: Smith Depp DiCaprio Pitt Chan Ford Case 2: Case 3: Freeman Stallone Bale 9