#K51312. Minimum Steps to Treasure

    ID: 29060 Type: Default 1000ms 256MiB

Minimum Steps to Treasure

Minimum Steps to Treasure

You are given a grid with dimensions (N) (rows) and (M) (columns). Each cell in the grid is either an open land ('O'), an obstacle ('X'), or contains a treasure ('T'). Starting from a given position ((r_0, c_0)), your task is to determine the minimum number of steps required to reach any treasure cell. You can move in four directions: up, down, left, or right. Moving through obstacles is not allowed. If there is no possible path to any treasure, output (-1). This problem can be solved using a breadth-first search (BFS) algorithm.

inputFormat

Input is read from standard input. The first line contains two integers (N) and (M), denoting the number of rows and columns, respectively. The second line contains two integers (r_0) and (c_0), representing the starting row and column (0-indexed). The following (N) lines each contain a string of length (M) describing the grid. Each character is either 'O', 'X', or 'T'.

outputFormat

Output a single integer to standard output: the minimum number of steps required to reach a treasure cell. If no path exists, output (-1).## sample

5 5
0 0
OOOXT
OXOOO
OOXTO
TOOXO
OOOOO
3