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*

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值