#K77432. Path Finding in a Matrix

    ID: 34863 Type: Default 1000ms 256MiB

Path Finding in a Matrix

Path Finding in a Matrix

You are given a matrix of size \(r \times c\) consisting of characters 'O' and 'X'. Your task is to determine whether there exists a path from the top-left cell (0, 0) to the bottom-right cell (\(r-1\), \(c-1\)) such that all visited cells contain 'O'. You may move in four directions: up, down, left, and right.

Important: The starting cell and the destination cell must be 'O'. If either is 'X', the answer is immediately False.

Use an algorithm such as Depth-First Search (DFS) to check the connectivity through valid cells.

inputFormat

The first line contains two integers r and c separated by a space. Each of the next r lines contains a string of length c consisting solely of the characters 'O' and 'X'.

outputFormat

Print "True" (without quotes) if there is a valid path from the top-left cell to the bottom-right cell, otherwise print "False".## sample

4 4
OXOO
OOXO
XOXO
OOOO
True