#K49467. Minimum Distance Between Metallic Objects
Minimum Distance Between Metallic Objects
Minimum Distance Between Metallic Objects
You are given an n x m grid containing characters. Each cell of the grid contains either a dot .
or an uppercase letter M
representing a metallic object. Your task is to compute the minimum Manhattan distance between any two metallic objects.
The Manhattan distance between two points \((r_1, c_1)\) and \((r_2, c_2)\) is defined as:
\( |r_1 - r_2| + |c_1 - c_2| \)
You must read the grid from the standard input and output a single integer representing the minimum distance between any two metallic objects.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains two integers
n
andm
— the number of rows and columns in the grid, respectively. - The next
n
lines each contain a string ofm
characters where each character is either.
orM
.
It is guaranteed that there are at least two metallic objects (M
) in the grid.
outputFormat
Output to standard output (stdout) a single integer — the minimum Manhattan distance between any two metallic objects in the grid.
## sample3 4
....
.M..
...M
3