
The drone must complete a journey in two parts:
The drone's movement capabilities change after it picks up the package:
Your task is to find the minimum total number of steps required for the entire journey (S -> P -> D).
The first line of input contains two space-separated integers, 'N' and 'M', the dimensions of the grid.
The next 'N' lines each contain a string of 'M' characters, representing the grid.
Print a single integer representing the minimum total number of steps for the full journey.
If either part of the journey is impossible (e.g., 'P' is unreachable from 'S', or 'D' is unreachable from 'P'), print -1.
The problem can be solved by running two separate Breadth-First Search (BFS) traversals: one for the S->P leg with 4-directional movement, and a second for the P->D leg with 2-directional movement.
The grid is 0-indexed.
3 3
S.P
...
..D
4
- Path from S(0,0) to P(0,2): The shortest path is `right -> right`, which takes 2 steps.
- Path from P(0,2) to D(2,2): The shortest path is `down -> down`, which takes 2 steps.
- Total minimum steps = 2 + 2 = 4.
3 3
S.P
###
..D
-1
The path from S to P is possible (2 steps). However, after picking up the package at P, the drone's path to D is completely blocked by obstacles. Since the second leg of the journey is impossible, the entire journey is impossible.
The expected time complexity is O(N * M).
1 <= N, M <= 1000
The grid will contain exactly one 'S', one 'P', and one 'D'.
Time limit: 1 sec