Jetpack
InterviewTime limit1sMemory limit64 MB
Find the lexicographically smallest schedule of screen holds that moves Barry right across N columns through a 10-row grid while avoiding obstacles.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Greedy, Array, Simulation
- Solved
- No attempts yet
Problem
Mirko got a new phone for his birthday. Like every kid these days he downloaded all the popular mobile games, one of which has the hero running with a jetpack on his back.
In that game the hero, Barry, runs across a field of 10 rows and columns of equal squares. Barry starts in the bottom left square and runs to the right at one square per second, and he has to avoid the obstacles in his way.
While Mirko holds the screen down, Barry turns on his jetpack and climbs one row per second. He keeps moving right at the same time, so he travels diagonally at 45 degrees. Once he reaches the top row he runs right along it until Mirko lets go. When Mirko releases the screen, Barry falls one row per second, and once he reaches the bottom row he runs right along that one.
Stated exactly: during second , counting from 0, Barry moves from column to column . If the screen is held during that second he also moves one row up, or stays in the top row if he is already there. If the screen is not held he moves one row down, or stays in the bottom row if he is already there. Every square Barry passes through must be free of obstacles.
Mirko started playing only recently and is still bad at it. Given the layout of the field, work out when he has to hold the screen so that Barry crosses all columns.
Input
The first line contains the integer , the width of the field ().
Each of the next 10 lines contains characters, either X or .. An X is an obstacle and a . is a square Barry may pass through. The first of those lines is the top row of the field and the last one is the bottom row. Barry starts in the first square of the bottom row, and that square has no obstacle. Every input admits at least one way to finish the game.
Output
Several schedules can work, so exactly one of them is required. Write a schedule as a string of length where, counting from 0, character of is 1 if the screen is held during second and 0 if it is not. Among all strings that let Barry finish, output the lexicographically smallest one. In other words, at every second release the screen whenever finishing the game is still possible afterwards.
Print the number of presses on the first line. Each of the next lines describes one press with two integers and : Mirko presses the screen at second and holds it for seconds. These are the maximal blocks of consecutive 1 characters in listed in chronological order, so and . When , print a single line containing 0.