题目描述
Given an N x M matrix, with a few hurdles(denoted by 0) arbitrarily placed, calculate the length of the longest possible route possible from source(xs,ys) to a destination(xd,yd) within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contain any diagonal moves and a location once visited in a particular path cannot be visited again.If it is impossible to reach the destination from the source return -1.
Example 1:
Input:
{xs,ys} = {0,0}
{xd,yd} = {1,7}
matrix = 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
Output: 24
Explanation:
Example 2:
Input:
{xs,ys} = {0,3}
{xd,yd} = {2,2}
matrix = 1 0 0 1 0
0 0 0 1 0
0 1 1 0 0
Output: -1
Explanation:
We can see that it is impossible to
reach the cell (2,2) from (0,3).
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPath() which takes matrix ,source and destination as input parameters and returns an integer denoting the longest path.
Expected Time Complexity: O(2^(NM))
Expected Auxiliary Space: O(NM)
Constraints:
1 <= N,M <= 10
输入格式
见题面
输出格式
见题面