The Great Chambers of the Pyramid of Cheops#11848

The Great Chambers of the Pyramid of Cheops (Editorial)

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

(x,y,mask),\qquad 1\le x\le n,\;1\le y\le m,\;0\le mask<64.

Run BFS because every move costs 1. From (x,y,mask) inspect the four neighbours (x',y'):

Queue starts with (1,1,0). A 3-D array dist[x][y][mask] stores distances, initialised to . After the queue is exhausted:

\texttt{ans}= \min_{mask} dist[n][m][mask].

If ans it is the minimum steps; otherwise output . Complexity O(2^{6}nm).