Problem
Given an m x n
grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of grid[i][j]
can be:
1
which means go to the cell to the right. (i.e go fromgrid[i][j]
togrid[i][j + 1]
)2
which means go to the cell to the left. (i.e go fromgrid[i][j]
togrid[i][j - 1]
)3
which means go to the lower cell. (i.e go fromgrid[i][j]
togrid[i + 1][j]
)4
which means go to the upper cell. (i.e go fromgrid[i][j]
togrid[i - 1][j]
)
Notice that there could be some signs on the cells of the grid that point outside the grid.
You will initially start at the upper left cell (0, 0)
. A valid path in the grid is a path that starts from the upper left cell (0, 0)
and ends at the bottom-right cell (m - 1, n - 1)
following the signs on the grid. The valid path does not have to be the shortest.
You can modify the sign on a cell with cost = 1
. You can modify the sign on a cell one time only.
Return the minimum cost to make the grid have at least one valid path.
Examples
Example 1:
$$ \begin{matrix} \rightarrow & \rightarrow & \rightarrow & \rightarrow \\ \leftarrow & \leftarrow & \leftarrow & \leftarrow \\ \rightarrow & \rightarrow & \rightarrow & \rightarrow \\ \leftarrow & \leftarrow & \leftarrow & \leftarrow \\ \end{matrix} $$
|
|
Example 2:
$$ \begin{matrix} \rightarrow & \rightarrow & \downarrow \\ \downarrow & \leftarrow & \leftarrow \\ \rightarrow & \rightarrow & \uparrow \\ \end{matrix} $$
|
|
Example 3:
$$ \begin{matrix} \rightarrow & \leftarrow \\ \uparrow & \downarrow \\ \end{matrix} $$
|
|
Solution
Method 1 - Using Dijkstra Algorithm
To solve the problem of finding the minimum cost to make the grid have at least one valid path, we can use a shortest path algorithm like Dijkstra’s but slightly modified. The grid can be considered as a weighted graph where:
- Nodes represent grid cells.
- Edges between nodes represent possible moves between cells, with weights being the cost of modifying the signs.
Approach
- Define the direction vectors for moving right, left, down, and up.
- Use a priority queue (min-heap) to always expand the least cost cell first.
- Initialise the distance to reach each cell as infinity, except for the starting cell which is 0.
- Use the priority queue to explore the neighbors of the current cell.
- Calculate the cost to move to each neighbor. If it follows the current cell’s direction, the cost is 0, otherwise it is 1.
- Update the distance and push the neighbor to the queue if a shorter path is found.
- Continue until reaching the bottom-right cell.
- The final answer will be the distance to the bottom-right cell.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(m * n * log(m * n))
due to the use of a priority queue where every node is processed once. - 🧺 Space complexity:
O(m * n)
for storing distances and the priority queue.