<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:
    int dirs[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
    string boardToString(vector<vector<int>>& board){
        string str = "";
        for(int i = 0; i < 2; i++){
            for(int j = 0; j < 3; j++){
                str += to_string(board[i][j]);
            }
        }
        return str;
    }

    vector<string> getNexts(string str){
        vector<string> nexts;
        vector<vector<int>> cur = stringToBoard(str);
        int zero;
        for(zero = 0; zero < 6; zero++){
            if(cur[zero / 3][zero % 3] == 0)
                break;
        }
        int zx = zero / 3, zy = zero % 3;
        for(int d = 0; d < 4; d++){
            int nextx = zx + dirs[d][0];
            int nexty = zy + dirs[d][1];
            if(inArea(nextx, nexty)){
                swap(cur, zx, zy, nextx, nexty);
                nexts.push_back(boardToString(cur));
                swap(cur, zx, zy, nextx, nexty);
            }
        }
        return nexts;
    }

    vector<vector<int>> stringToBoard(string str){
        vector<vector<int>> board(2, vector<int>(3, 0));
        for(int i = 0; i < 6; i++){
            board[i / 3][i % 3] = str[i] - '0';
        }
        return board;
    }

    bool inArea(int x, int y){
        return x >= 0 && x < 2 && y >= 0 && y < 3;
    }

    void swap(vector<vector<int>>& board, int x1, int y1, int x2, int y2){
        int t = board[x1][y1];
        board[x1][y1] = board[x2][y2];
        board[x2][y2] = t;
    }
public:
    int slidingPuzzle(vector<vector<int>>& board) {
        queue<string> q;
        unordered_map<string, int> visited;

        string initialState = boardToString(board);
        if(initialState == "123450") return 0;
        
        // BFS
        q.push(initialState);
        visited[initialState] = 0;

        while(!q.empty()){
            string cur = q.front();
            q.pop();

            vector<string> nexts = getNexts(cur);
            for(string& next: nexts){
                if(visited.find(next) == visited.end()){
                    q.push(next);
                    visited[next] = visited[cur] + 1;
                    if(next == "123450") return visited[next];
                }
            }
        }

        return -1;
    }
};
17796