Lintcode 950 · Sliding Puzzle III (滑动拼图,BFS 好题)

950 · Sliding Puzzle III
Algorithms
Hard

Description
Given a 3 x 3 matrix, the number is 1~9, among which 8 squares have numbers, 1~8, and one is null (indicated by 0), asking if the corresponding number can be put on the corresponding label In the grid (spaces can only be swapped with up, down, left, and right positions), if it can output “YES”, otherwise it outputs “NO”.

Only $39.9 for the “Twitter Comment System Project Practice” within a limited time of 7 days!

WeChat Notes Twitter for more information(WeChat ID jiuzhang15)

Nothing

Example
Example 1:

Given matrix =[[1,2,3],[4,0,6],[7,5,8]]
return "YES"

Explanation:
First exchange 5 with a space, then 8 with a space exchange.
Example 2:

Given matrix =[[1,2,3],[4,5,6],[7,0,8]]
return "YES"

Explanation:
Just swap 8 with the space.

解法1:

class Solution {
public:
    /**
     * @param initState: the initial state of chessboard
     * @param finalState: the final state of chessboard
     * @return: return an integer, denote the number of minimum moving
     */
    string jigsawPuzzle(vector<vector<int>> &matrix) {
        string srcState = matrix2Str(matrix);
        //find the missing number
        int xorRes = 0;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                xorRes ^= matrix[i][j];
            }
        }
        for (int i = 1; i <= 9; i++) {
            xorRes ^= i;
        }
        string destState = "123456789";
        destState[xorRes - 1] = '0';
        queue<string> stateQueue;
        set<string> visited;
        stateQueue.push(srcState);
        visited.insert(srcState);
        int dx[4] = {1, -1, 0, 0};
        int dy[4] = {0, 0, 1, -1};
        int step = 0;
        while (!stateQueue.empty()) {
            int qSize = stateQueue.size();
            for (int i = 0; i < qSize; i++) {
                string frontState = stateQueue.front();
                stateQueue.pop();
                if (frontState == destState) return "YES";
                int pos0 = frontState.find('0');
                int x = pos0 / 3, y = pos0 % 3;
            
                for (int j = 0; j < 4; j++) {
                    int newX = x + dx[j];
                    int newY = y + dy[j];
                    
                    if (newX >= 0 && newX < 3 && newY >= 0 && newY < 3) {
                        string newState = frontState;
                        swap(newState[pos0], newState[newX * 3 + newY]);
                        if (visited.find(newState) == visited.end()) {
                            stateQueue.push(newState);
                            visited.insert(newState);
                        }
                    }
                }
            }
            step++;
        }
        return "NO";
    }
private:
    string matrix2Str(vector<vector<int>> &initState) {
        string str = "";
        int nRow = initState.size(), nCol = initState[0].size();
        for (int i = 0; i < nRow; i++) {
            for (int j = 0; j < nCol; j++) {
                str += '0' + initState[i][j];
            }
        }
        return str;
    }
};

解法2:DFS
解法3:A*

你可能感兴趣的:(leetcode,算法)