You are given a 2D grid of size M × N. The grid contains three kinds of cells:
# – an obstacle (impassable) (whitespace) – a road (passable)P – a point of interest (passable)Your task is to choose a single cell to place a camp. The camp must be reachable from every P and should minimize the sum of Manhattan distances to all P cells. Manhattan distance between two cells (r1, c1) and (r2, c2) is |r1 - r2| + |c1 - c2|.
If multiple cells achieve the same minimum total distance, pick the one with the smallest row index; if there is still a tie, pick the one with the smallest column index.
You may assume that every P is reachable from the chosen camp cell.
M and N – the number of rows and columns.M lines: each line is a string of length N representing a row of the grid.Print two space-separated integers: the row and column indices (0‑based) of the best camp location.
Input:
5 5
#####
# P #
# #
#P P#
#####
Output:
2 2
Explanation: The cell at (2,2) is the only road cell that is reachable from all three P points. Its total Manhattan distance to the Ps is minimized.
Input:
3 4
# P
P #
P
Output:
1 1
Explanation: The cell (1,1) is a road cell reachable from all three Ps. The sum of distances from (1,1) to the Ps at (0,2), (1,0), and (2,3) is |1-0|+|1-2| + |1-1|+|1-0| + |1-2|+|1-3| = 1+1 + 0+1 + 1+2 = 6, which is the smallest possible total.
#, (space), and P.P.