I was solving the following problem but couldn't even think how to optimize it:
Description
Given a maze maze of n*m, in the maze, . indicates empty space, S indicates the start point, T indicates the end point, * means the obstacle, and the numbers 0~9 indicates different treasures. Your journey will finish when you arrive at the end point with all treasures. Please output the minimal number of steps you will take.If you can never finish your journey, output -1
Given a maze, whose size is n * m. In the maze:
. indicates empty space
S indicates the start point
T indicates the end point
* means the obstacle
The numbers 0~9 indicates different treasures
Starting from S, how many steps at least do you need to reach T with getting all the treasures? If there
is no solution, return -1.Note
Examples
Example 1:
Input:["T1S.",".*0*","....","..*."]
Output:4
Explanation:(0,2)->(1,2)->(0,2)->(0,1)->(0,0)Example 2:
Input:["1*S.","..0.","T..."]
Output:6
Explanation:(0,2)->(1,2)->(1,1)->(1,0)->(0,0)->(1,0)->(2,0)