Candy in the Maze
Time limit2sMemory limit1024 MB
Find the longest lollipop that Ralph can carry from the left column to the right column through a grid maze with walls.
- Level
Medium6 of 10
- Topics
- Graph, Union-find, Matrix
- Solved
- No attempts yet
Statement
Ralph and Vanellope found a long lollipop that they need to build a machine. Once they found it, they realized the only way back is through a maze. Of course, they have to take the lollipop with them.
The maze is a grid of by cells. Each cell is either empty or contains a maze wall. The lollipops at this place come in different lengths, but each one occupies consecutive cells in a single line for some . A lollipop cannot be placed on a cell that contains a wall. Ralph is very strong, so he can carry a lollipop of any length.
At first, Ralph can enter the maze at any cell in the leftmost column, but he must hold the lollipop parallel to the left border of the maze. To exit, he must reach any cell in the rightmost column while holding the lollipop parallel to that border.
While Ralph holds the lollipop parallel to one of the borders, he can move it one cell along that border. Alternatively, he can grab the lollipop by one of its ends, lift it into the air at that spot, and lower it parallel to the other side of the maze. He can do these actions only if the lollipop ends up on empty cells afterward.
Ralph and Vanellope want to know the maximum length of a lollipop that can be carried through the maze.
Input
The first line contains two integers and , the number of rows and columns in the maze (). The next lines each contain characters. The -th character of the -th line is \# if cell contains a wall, and . otherwise.
Output
Print one integer, the maximum number of cells a lollipop can occupy such that Ralph can carry it through the maze. If no lollipop length can be carried through the maze, print .