<p>On an <code>2 x 3</code> board, there are five tiles labeled from <code>1</code> to <code>5</code>, and an empty square represented by <code>0</code>. A <strong>move</strong> consists of choosing <code>0</code> and a 4-directionally adjacent number and swapping it.</p>

<p>The state of the board is solved if and only if the board is <code>[[1,2,3],[4,5,0]]</code>.</p>

<p>Given the puzzle board <code>board</code>, return <em>the least number of moves required so that the state of the board is solved</em>. If it is impossible for the state of the board to be solved, return <code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/29/slide1-grid.jpg" style="width: 244px; height: 165px;" />
<pre>
<strong>Input:</strong> board = [[1,2,3],[4,0,5]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Swap the 0 and the 5 in one move.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/29/slide2-grid.jpg" style="width: 244px; height: 165px;" />
<pre>
<strong>Input:</strong> board = [[1,2,3],[5,4,0]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> No number of moves will make the board solved.
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/29/slide3-grid.jpg" style="width: 244px; height: 165px;" />
<pre>
<strong>Input:</strong> board = [[4,1,2],[5,0,3]]
<strong>Output:</strong> 5
<strong>Explanation:</strong> 5 is the smallest number of moves that solves the board.
An example path:
After move 0: [[4,1,2],[5,0,3]]
After move 1: [[4,1,2],[0,5,3]]
After move 2: [[0,1,2],[4,5,3]]
After move 3: [[1,0,2],[4,5,3]]
After move 4: [[1,2,0],[4,5,3]]
After move 5: [[1,2,3],[4,5,0]]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>board.length == 2</code></li>
	<li><code>board[i].length == 3</code></li>
	<li><code>0 &lt;= board[i][j] &lt;= 5</code></li>
	<li>Each value <code>board[i][j]</code> is <strong>unique</strong>.</li>
</ul>

class Solution {
private:
    uint32_t encode_config(const vector<vector<int>>& config) {
        return config[0][0] | (config[0][1] << 4) | (config[0][2] << 8) |
            (config[1][0] << 12) | (config[1][1] << 16) | (config[1][2] << 20);
    }

    uint32_t encode_state(const array<uint8_t, 6>& state) {
        return state[0] | (state[1] << 4) | (state[2] << 8) |
            (state[3] << 12) | (state[4] << 16) | (state[5] << 20);
    }

    void decode_state(uint32_t config, array<uint8_t, 6>& state, size_t& i, size_t& j) {
        state[0] = (config & 0xF);
        state[1] = ((config >> 4) & 0xF);
        state[2] = ((config >> 8) & 0xF);
        state[3] = ((config >> 12) & 0xF);
        state[4] = ((config >> 16) & 0xF);
        state[5] = ((config >> 20) & 0xF);

        for (size_t k = 0; k < 6; ++k) {
            if (state[k] == 0) {
                i = k / 3;
                j = k % 3;
            }
        }
    }

public:
    int slidingPuzzle(vector<vector<int>>& board) {
        const uint32_t target_config = encode_config({{1, 2, 3}, {4, 5, 0}});
        array<uint8_t, 6> state;
        unordered_set<uint32_t> visited;
        queue<pair<uint32_t, int>> q;
        
        q.emplace(encode_config(board), 0);

        while (!q.empty()) {
            uint32_t config = q.front().first;
            int steps = q.front().second;

            q.pop();

            if (config == target_config) {
                return steps;
            }

            if (visited.contains(config)) {
                continue;
            }

            visited.insert(config);

            size_t i, j;
            decode_state(config, state, i, j);

            if (j > 0) {
                swap(state[3 * i + j], state[3 * i + j - 1]);
                q.emplace(encode_state(state), steps + 1);
                swap(state[3 * i + j], state[3 * i + j - 1]);
            }

            if (j < 2) {
                swap(state[3 * i + j], state[3 * i + j + 1]);
                q.emplace(encode_state(state), steps + 1);
                swap(state[3 * i + j], state[3 * i + j + 1]);
            }

            if (i == 0) {
                swap(state[3 * i + j], state[3 * (i + 1) + j]);
                q.emplace(encode_state(state), steps + 1);
                swap(state[3 * i + j], state[3 * (i + 1) + j]);
            } else {
                swap(state[3 * i + j], state[3 * (i - 1) + j]);
                q.emplace(encode_state(state), steps + 1);
                swap(state[3 * i + j], state[3 * (i - 1) + j]);
            }
        }

        return -1;
    }
};
10434