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

/*
==============
Sliding Puzzle
==============

On an 2 x 3 board, there are five tiles labeled from 1 to 5, and an empty square represented by 0. A move consists of choosing 0
and a 4-directionally adjacent number and swapping it.
The state of the board is solved if and only if the board is [[1,2,3],[4,5,0]].
Given the puzzle board board, return the least number of moves required so that the state of the board is solved. If it is impossible
for the state of the board to be solved, return -1.

Example 1:

Input: board = [[1,2,3],[4,0,5]]
Output: 1
Explanation: Swap the 0 and the 5 in one move.

Example 2:

Input: board = [[1,2,3],[5,4,0]]
Output: -1
Explanation: No number of moves will make the board solved.

Example 3:

Input: board = [[4,1,2],[5,0,3]]
Output: 5
Explanation: 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]]

Constraints:

- board.length == 2
- board[i].length == 3
- 0 <= board[i][j] <= 5
- Each value board[i][j] is unique.
*/

/*
========
Solution
========

Complexity:
*/

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) {
        for (size_t k = 0; k < 6; ++k) {
            state[k] = (config >> (k * 4)) & 0xF;
            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}});
        pair<int, int> offsets[4] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        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);

            for (size_t k = 0; k < 4; ++k) {
                int ni = i + offsets[k].first;
                int nj = j + offsets[k].second;

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

        return -1;
    }
};
10434