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
4mo

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!
Select
Add answer anonymously...

PhonePe Software Developer interview questions & answers

A Software Developer was asked 6mo agoQ. Why do you want to join?
A Software Developer was asked 8mo agoQ. Given a graph, write a function to perform a Breadth-First Search (BFS) traversa...read more
A Software Developer was asked Q. Insertion in AVL Tree Problem Statement You are required to implement an AVL Tre...read more

Popular interview questions of Software Developer

A Software Developer was asked 6mo agoQ1. Why do you want to join?
A Software Developer was asked Q2. Insertion in AVL Tree Problem Statement You are required to implement an AVL Tre...read more
A Software Developer was asked Q3. Unlock the Briefcase Problem Statement You have a briefcase secured by a lock wi...read more
PhonePe Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits