Collect All Keys Problem

Collect All Keys Problem — ExecCode Hard DSA Practice

Solve the Collect All Keys problem on ExecCode. Free online hard DSA practice in Graph. Write and run code in Java, C++, Python — no signup required to run.

Problem description

A maze contains walls, keys (a-f) and matching locks (A-F). You start at '@'. You may walk through a lock only after picking its key. Return the minimum steps to collect every key. Return -1 if impossible. You are given: grid — list of strings or 2D chars with '.', '#', '@', keys and locks Return integer steps or -1. Locks without their key block movement. Keys can be collected in any order.

Examples

Input {'grid': ['@.a.#', '###.#', 'b.A.B']}; Output 8. Input {'grid': ['@..aA']}; Output 3. Input {'grid': ['@#a']}; Output -1

Constraints

Walking onto a key collects it immediately

Practice Collect All Keys free on ExecCode. Browse DSA problems, topic map, and placement guides.