Gridlock
InterviewTime limit6sMemory limit1024 MB
Given a grid of arrow blocks, find an order that slides every block out of the grid in its arrow direction, or report that no such order exists.
- Level
Medium6 of 10
- Topics
- Topological sort, Greedy, Implementation
- Solved
- No attempts yet
Problem
The Foolish Puzzle Company posted an advertisement for a new puzzle that shows a very inefficient way of playing. Taunted by the "99.99% cant solve these puzle" caption, you downloaded the puzzle. After wasting a good amount of time, you noticed the levels keep getting larger and the game is no longer fun. So you decide to write a program that solves the levels automatically and skips the impossible ones.
A level is a rectangular grid of blocks. Each block has an arrow pointing up, down, left, or right. You remove a block by sliding it in the direction of its arrow. A block must slide all the way out of the grid. You cannot slide it partway, slide another block, and then slide the first block out later.
Find an order that removes all the blocks, or state that it is impossible to remove all of them.
Input
The first line contains two integers and (), the number of rows and columns of the grid.
The next lines each contain a string of length made of the characters "<", "^", ">", and "v". Each character gives the direction of the block.
Output
If the grid can be solved, print pairs of numbers and (), one pair per block, in the order the blocks are removed. Each pair is the row and column of the block.
If it is impossible to solve the grid, print "impossible".
If there are multiple valid solutions, you may print any of them.