advertisement

How to Pebbling Odometer And beat Gennady Korotkevich Pebbling Odometer (IOI 2012 #1) • IOI problems are not that hard* • This problem is a variation on Karel o The robot taught in Java o Can't do much except turn, move, and get/put beepers • The language specification is much more limited than Java's, however • Very ad-hoc problem o Tripped up Gennady Korotkevich (IOI 1st place 2009-2011) o Only one perfect score on this problem • Not Johnny Ho (missed .14 points) • Score rounds to nearest integer :D Basic Operations • • • • • • • • • Your odometer starts at (0, 0) and faces north The +x axis points down and the +y axis points right left - turn left right - turn right (nicer than Karel!) move - move forward one get - remove one pebble put - put one pebble halt - exit # can be used as comment Flow Operations • [label name]: - this defines a label (position marker) • jump [label name] - move program execution to label • border [label name] - if program is facing border, jump to label • pebble [label name] - if current position has at least one pebble, jump to label Basic loop start: pebble endloop move jump start endloop: # this program moves the odometer forward until it lands on a pebble Another example start: pebble getpebble_cont jump endloop getpebble_cont: get jump start endloop: # this program removes all pebbles from current position Alright... subtask #1 [9 pts] At the beginning there are x pebbles in cell (0, 0) and y in cell (0, 1), whereas all the other cells are empty. Write a program that terminates with the odometer in cell (0, 0) if x ≤ y, and in cell (0, 1) otherwise. Solution • Take pebble from (0, 0), and then take pebble from (0, 1) • Repeat • If it's ever not possible to take a pebble from the position you’re currently on, exit. Subtask #3 [19 pts] There are exactly two pebbles somewhere in row 0: one is in cell (0, x), the other in cell (0, y); x and y are distinct, and x + y is even. Write a program that leaves the odometer in cell (0, (x + y) / 2), i.e., exactly in the midpoint between the two cells containing the pebbles. Solution • • • • Find first pebble (on the left) Add pebble to its right Find next pebble (on the right) Add pebble to its left o unless there's a pebble there already, in which case you exit • Repeat Subtask #5 [32 pts] There may be any number of pebbles in each cell of the grid (between 0 and 15). Write a program that finds the minimum, i.e., that terminates with the odometer in a cell (i, j) such that every other cell contains at least as many pebbles as (i, j). After running the program, the number of pebbles in each cell must be the same as before running the program. Graded based on # of commands in program :) Target was 444, Johnny got 449 Solution • First case: minimum # of pebbles is 0 o Scan through all locations in zigzag pattern o If you find a location that has 0 pebbles, exit • Second case: minimum # of pebbles is 1 o o o o For each location: Take away 1 pebble If the location has no pebbles left, put back pebbles and exit Put back pebbles • [Repeat 13 more times] Solution [cont.] • Optimally, make a program that generates your program (label names must be unique) • Need some more work to minimize code size o Cascade putting back pebbles when exiting PotW • 15 points: solve subtask #1 • 20 points: solve subtask #3 • Please note that you start facing north at (0, 0), and (x, y) means that the object is currently lying on row x and column y • We'll be somewhat lenient since an interpreter is not yet available o 30 points: write an interpreter for us too :)