Problem of the day
‘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'.
For every test case, return the length of the shortest path to a service station. If no such path is available, return -1;
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