


‘GRID’ =

We cannot reach the cell ‘(2, 2)’ until the time is ‘5’. When the time is ‘5’, we move along the path:
0 -> 1 -> 4 -> 3 -> 5
The cell with value ‘1’ will be unlocked when the time is ‘1’.
At time = 1: 0 -> 1
Similarly, the cell with value ‘4’ is unlocked when the time is ‘4’. At that time the cell with value ‘3’ will also be unlocked, as it unlocks at time ‘3’.
At time = 4: 0 -> 1 -> 4 -> 3
The cell with value ‘5’ will be unlocked when the time is ‘5’.
At time = 5: 0 -> 1 -> 4 -> 3 -> 5
So, the least time to reach cell ‘(2, 2)’ is ‘5’.
1. All elements in the ‘GRID’ are unique.
2. ‘GRID[i][j]’ is a permutation of [0, 1, … , (N ^ 2) - 1].
The first line of input contains an integer ‘T’ which denotes the number of test cases. Then, the ‘T’ test cases follow.
Each test case’s first line contains an integer ‘N’ denoting the number of rows and columns in ‘GRID’. Then, ‘N’ lines follow.
Each line contains ‘N’ space-separated integers denoting the row values of ‘GRID’.
For every test case, print a single line containing a single integer denoting the least time required to reach the cell ‘(N - 1, N - 1)’.
The output of each test case will be printed in a separate line.
You do not need to input or print anything, as it has already been taken care of. Just implement the given function.
1 <= T <= 10
2 <= N <= 50
0 <= Value in each element of ‘GRID’ <= (N ^ 2) - 1
Where ‘T’ is the number of test cases, and ‘N’ is the number of rows and columns in ‘GRID’.
Time limit: 1 second
A simple approach will be to assume the time required to reach ‘(n - 1, n - 1)’ as ‘0’. Now traverse the grid from ‘(0, 0)’ and see if we can reach the last cell. If we cannot reach the last cell, then increment the time required by ‘1’ and keep increasing the time until we can reach the last cell. Following is an implementation using BFS traversal:
The ‘bfs(integer time)’ function:
Instead of linearly searching for the least time value, we can do a binary search. In binary search, if we cannot reach the last cell for the current value of time (i.e., ‘mid’), we search for a greater time value because more cells get unlocked as time passes. If we reach a value of time for which the last cell is reachable, we store this value as the answer and search if an even lower value of time is possible. In the following implementation, we use BFS to check if the last cell is reachable.
The ‘reachable(integer time)’ function using BFS:
Consider an approach similar to Dijkstra’s shortest path algorithm, in each iteration instead of adding the adjacent node with a smaller edge value, we add the node with a smaller node value (i.e., unlock time). In this way, the time required to reach the last node ‘(n - 1, n - 1)’ will be the least possible time value.
Use a priority queue (in this case a min-heap) to maintain a list of cells that are adjacent to the visited cells. Maintain a variable ‘time’ which stores the time required to reach the visited cells. Initially, ‘time = grid[0][0] as only the cell ‘(0, 0)’ is visited, push all its adjacent cells in the priority queue. In each subsequent iteration, pop the cell with the smallest unlock time, add its adjacent cells that are unvisited to the priority queue, and if its unlock time is more than ‘time’ the update ‘time’. Stop the algorithm when we reach the cell ‘(n - 1, n - 1)’ and update ‘time’, the new ‘time’ value will be the answer.
Minimize Maximum Adjacent Distance
Second Largest in Tree Level
Sorted Doubly Linked List to Balanced BST
Window Median Cost
Minimized Maximum of Products Distributed to Any Store