A taxi can take multiple passengers to the railway station at the same time. On the way back to the starting point, the taxi driver may pick up additional passengers for his next trip to the airport. A map of passenger location has been created, represented as a square matrix.
The matrix is filled with cells, and each cell will have an integer value as follows:
The rules of motion of taxi are as follows:
For example, consider the following grid:
Start at the top left corner. Move right one, collecting a rider. move down one to the airport. Cell (1, 0) is blocked, so the return path is the reversed of the path to the airport. All paths have been explored, and 1 rider was collected.
Returns
int
: maximum number of passengers that can be collected.
🌿 𖡼𖤣𖥧𖡼𓋼𖤣𖥧𓋼𓍊, Credit to robotᥫ᭡.🌿
Example 1:
Input: mat = [[0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Output: 2
Explanation:The driver can contain a maximum of 2 passengers by taking the following path: (0,0) -> (0,1) -> (0,2) -> (0,3) -> (1,3) -> (2,3) -> (3,3) -> (3,2) -> (3,1) -> (3,0) -> (2,0) -> (1,0) -> (0,0)
Example 2:
Input: mat = [[0, 1, -1], [1, 0, -1], [1, 1, 1]]
Output: 5
Explanation:The driver can contain a maximum of 5 passengers by taking the following path: (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (2,1) -> (2,0) -> (1,0) -> (0,0)
1 <= n <= 100
-1 <= mat[i][j] <= 1


input:
output: