Under construction

Still being built. Things may change

Right Angle Painting

Time limit4sMemory limit256 MB

Summary
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 N×NN \times N grid, and some cells (possibly none) contain obstacles.

The grid is given as NN strings S1,…,SNS_1, \ldots, S_N. The jj-th character of SiS_i describes cell (i,j)(i, j): '.' 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

NN S1S_1 S2S_2 ⋮\vdots SNS_N

Output

Print "POSSIBLE" if he can paint all cells without obstacles. Otherwise print "IMPOSSIBLE".

Constraints

  • 1≤N≤4001 \leq N \leq 400
  • ∣Si∣=N|S_i| = N
  • Each character of SiS_i is '.', '#', or 's'.
  • There is exactly one 's'.
  • There is at least one '.'.

Examples2

  1. Example 1

    Input
    5
    #....
    ..s..
    ..#..
    #....
    ##..#
    
    Expected output
    POSSIBLE
    
  2. Example 2

    Input
    5
    s.###
    ..###
    ###..
    #....
    #..##
    
    Expected output
    IMPOSSIBLE