Robot
Time limit1sMemory limit128 MB
Write the shortest down-right program of at most k steps whose repetition exits the board without hitting an obstacle, ties broken lexicographically.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Math, Greedy
- Solved
- No attempts yet
Problem
A child received a programmable robot as a gift. The robot can store a program, which is a sequence of at most moves. When it is turned on, the robot performs the moves of the program one after another. After finishing the last move it returns to the start of the program and repeats the whole sequence again, looping forever.
The robot is placed on the top-left square of an board. Some of the other squares hold obstacles that the robot may not enter. Your task is to write a program for the robot so that, by repeating it, the robot eventually walks off the board.
Only two kinds of moves are allowed: step one square down, or step one square to the right. In the program the character '1' means a step down and the character '0' means a step to the right. The robot walks off the board as soon as a move would take it past the bottom edge or past the right edge. The robot must never step onto an obstacle.
Input
The first line contains two integers and (, ), where is the side length of the board.
Each of the next lines contains a string of characters describing one row of the board. The character 'R' marks the robot's starting square, which is always the top-left corner. The character '.' marks a free square, and the character 'X' marks a square that holds an obstacle.
You may assume that a program of length at most that lets the robot leave the board always exists.
Output
Print a single line: the program as a string of at most characters, where '0' is a step to the right and '1' is a step down.
If more than one program works, print the shortest one. If more than one shortest program works, print the lexicographically smallest among the shortest.