Right Angle Painting
Time limit4sMemory limit256 MB
Given an N by N grid with obstacles and a start cell, decide whether a path can paint every free cell while alternating horizontal and vertical moves.
- Level
Medium7 of 10
- Topics
- Graph, Greedy, Implementation
- Solved
- No attempts yet
Problem
Takahashikun likes to paint floors. The floor is an grid, and some cells (possibly none) contain obstacles.
The grid is given as strings . The -th character of describes cell : '.' and 's' are empty cells, and '#' is a cell with an obstacle.
Exactly one cell contains 's'. Takahashikun enters that cell and paints it. Then he makes zero or more steps by these rules:
- Each step moves to one of the four adjacent cells (up, down, left, right) and paints it.
- Except for the first step, the direction must change by 90 degrees from the previous step. After a horizontal move he must move vertically, and vice versa.
- He must not enter an already painted cell.
- He must not enter an obstacle.
- He must not leave the grid.
Input
Output
Print "POSSIBLE" if he can paint all cells without obstacles. Otherwise print "IMPOSSIBLE".
Constraints
- Each character of is '
.', '#', or 's'. - There is exactly one '
s'. - There is at least one '
.'.