Minimum Moves to Collect All Keys
Given an 'N' x 'M' grid, find the minimum number of moves required to collect all keys when starting at a specified point. Each move allows you to travel to an adjacent cell (up, down, left, right). The grid contains walls, locks, keys, and a starting point, and you must adhere to specific movement rules.
Example:
Input:
GRID = [["@.aA"], [".B#."], ["...b"]]
Output:
5
Explanation:
Starting from '@', collect all keys in 5 moves without being hindered by locks if you have their respective keys.
Input:
The first line contains an integer 'T', the number of test cases.
For each test case:
- An integer 'N', the number of rows in the grid.
- Followed by 'N' strings, each string representing a row of the grid.
Output:
For each test case, output the minimum number of moves to collect all keys, or -1 if impossible.
Constraints:
- 1 <= T <= 10
- 1 <= N <= 100
- 1 <= M <= 100
- GRID[i][j] is either an English letter, '.', '#', or '@'.
- Each key is unique and has a matching lock.
- Alphabets for keys and locks follow English alphabetical order.
- The number of keys ranges from 1 to 6.
- Time Limit: 1 second
Note:
Implementation should only return results and not handle print logic.
AnswerBot
4d
Find the minimum number of moves required to collect all keys in a grid starting from a specified point.
Use breadth-first search (BFS) to explore all possible paths from the starting point to collect ...read more
Help your peers!
Add answer anonymously...
Top PhonePe Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in PhonePe Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app