Glen
Time limit1sMemory limit256 MB
Given a target pattern of marks on an N by M grid, produce the fixed boustrophedon walk that flips tiles down-and-back to match the pattern.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation, Greedy, Matrix
- Solved
- No attempts yet
Problem
Niko is trapped in a room in Glen. The floor of the room holds tiles laid out as a rectangle with rows and columns.
Stepping on a tile flips its mark. A tile with no mark gets one, and a tile that already has a mark loses it. To open the way out, the tiles must end up showing one given pattern of marks.
Every mark is erased before Niko starts moving. Niko starts on the square one step to the left of the upper left tile, which is outside the rectangle. Each move takes Niko one square up, down, left, or right. Niko may walk outside the rectangle, and a square outside it holds no tile, so stepping there flips nothing.
Niko can move at most three times the number of tiles, that is times.
Tell Niko how to move.
Input
The first line has two positive integers and (, ). is the number of rows and is the number of columns.
Each of the next lines describes one row of tiles. # means the tile must end up with a mark, and . means it must end up with no mark. At least one tile must end up with a mark.
Output
Print the order of Niko's moves on one line with no spaces. Use U for up, D for down, L for left, and R for right.
Many different walks produce the same pattern, so only the walk fixed by the rules below is accepted.
- Rows are handled from the top row down to the bottom row. Niko crosses the first row from left to right, the second row from right to left, and the direction alternates from one row to the next.
- Niko enters the upper left tile with a single
Rmove from the starting square. - Inside a row, Niko steps on the tiles one after another in that row's direction, one move per tile.
- Right after stepping on a tile, compare the tile's current mark with the mark it must show at the end. If they differ, Niko moves
Done square and thenUone square. If they match, Niko does not move down. Going down and back up steps on the tile again, which fixes its mark, and the square below is stepped on once, so its mark flips too. That square below is handled by the same rules when Niko crosses its row. - After the last tile of a row is handled, Niko moves
Done square onto the tile directly below, which is the first tile Niko steps on in the next row. Niko stops once the last tile of the bottom row is handled.
The walk built by these rules always exists and never takes more than moves, so there is no case without an answer.