To describe a state in the grid, we need to know not only Indiana's position but also which of the six scarabs have already been collected. Use a 6-bit mask mask\in[0,2^{6}), bit set if scarab 'a'+i is in Indiana’s inventory. A state is
Run BFS because every move costs 1. From (x,y,mask) inspect the four neighbours (x',y'):
#: blocked.
A...F: let i=c-\texttt{A}; allowed only if (mask\;\&\;(1\ll i))\neq0.
a...f: let i=c-\texttt{a}, set mask' = mask \mid (1\ll i).
.: free cell, mask unchanged.
Queue starts with (1,1,0). A 3-D array dist[x][y][mask] stores distances, initialised to . After the queue is exhausted:
If ans it is the minimum steps; otherwise output . Complexity O(2^{6}nm).