Problem of the day
Ninja was supposed to go on a road trip with his friends, but his car was damaged. Ninja wants to repair it as soon as possible. Help ninja to find the nearest service station. You are given a character matrix, ‘GRID’ of size ‘N x M’. The ‘grid’ contains the following characters:
The Ninja can only travel to adjacent free cells from his current location, i.e., in the North, South, East, or West direction. Your task is to find the length of the shortest path through which the Ninja can get to a service station. If there is no such path available, return -1.
Example:‘N’ = 4, ‘M’ = 6
Given ‘GRID’:
(Empty cells represent the character ‘O’)
The Ninja can reach the nearest service station by moving two cells south and then three cells east. So, the answer is ‘5’.
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 two space-separated integers, ‘N’ and ‘M’, denoting the number of rows and columns in the ‘GRID’.
The next ‘N’ lines contain ‘M’ characters denoting the elements of the 'GRID'.
Output format:
For every test case, return the length of the shortest path to a service station. If no such path is available, return -1;
Note:
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N, M <= 100
Value in each element of ‘GRID’ = {‘N’, ‘S’, ‘O’, ‘X’}
Time limit: 1 second
2
5 8
X X X X X X X X
X N O X O O S X
X O O X O X X X
X O O O O O S X
X X X X X X X X
5 6
X X X X X X
X N O X S X
X O O X O X
X O X S O X
X X X X X X
7
-1
Test Case 1:
‘N’ = 5, ‘M’ = 8
Given ‘GRID’:
(Empty cells represent the character ‘O’)
The Ninja can reach the nearest service station by moving two cells south and then five cells east. So, the answer is ‘7’.
Test Case 2:
‘N’ = 5, ‘M’ = 6
Given ‘GRID’:
(Empty cells represent the character ‘O’)
As the Ninja cannot reach any service station because of obstacles, the answer is ‘-1’.
2
4 5
X X X X X
X N O O O
X O O O S
X X X X X
3 4
N O S X
O O X X
S O X X
4
2
Consider the ‘GRID’ as an undirected graph.
Consider the given ‘GRID’ as an undirected graph where each cell represents a vertex, and if two free cells are adjacent, they are connected by an edge. Dijkstra’s algorithm is used for finding the shortest path between nodes in a weighted graph. As the given graph is unweighted, the path length is equal to the number of edges, so consider each edge’s weight to be ‘1’. Consider the cell with the ninja as the source node and break the algorithm when we reach a cell with a service station. There are ‘N x M’ vertices where each vertex is of the form ‘[r, c]’ (‘r’: row number, and ‘c’: column number). Below is an implementation of Dijkstra’s algorithm using priority queue(in this case: min-heap):
First find the presence of ninja:
Now apply Dijkstra’s algorithm to find the minimum cost path to reach the target:
O(M * N *(log(M) + log(N))), where ‘N’ is the number of rows and ‘M’ is the number of columns.
The time complexity of Dijkstra’s algorithm using priority queue is ‘O(V + E*log(E))’, where ‘V’ is the number of vertices and ‘E’ is the number of edges. For the given ‘GRID’ we have ‘V = M * N’ and ‘E = (2*M*N - M - N) = O(M * N)’. Therefore, the time complexity is ‘O(M * N * log(M*N)) = O(M*N*(log(M) + log(N)))’.
O(M*N), where ‘N’ is the number of rows and ‘M’ is the number of columns.
For Dijkstra’s algorithm, the maximum size of the priority queue can be ‘O(E)’ and the size of the ‘PATH’ array used to store the shortest path is ‘O(V)’, where ‘E’ is the number of edges and ‘V’ is the number of vertices. So, we need ‘O(E + V)’ space. For the given ‘N x M’ grid, we have ‘E = O(M * N)’ and ‘V = M * N’. Therefore, the space complexity is ‘O(M * N)’.