Leetcode 773. Sliding Puzzle

题目

On a 2x3 board, there are 5 tiles represented by the integers 1 through 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 a puzzle 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.

Examples:

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

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

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

Input: board = [[3,2,4],[1,5,0]]
Output: 14

Note:

  • board will be a 2 x 3 array as described above.
  • board[i][j] will be a permutation of [0, 1, 2, 3, 4, 5].

Solution

class Solution {
public:
    int slidingPuzzle(vector<vector<int>>& board) {
        string init = to_string(board[0][0]) + to_string(board[0][1]) + to_string(board[0][2])
                       + to_string(board[1][0]) + to_string(board[1][1]) + to_string(board[1][2]);
        
        vector<vector<int>> movePos{{1,3},{0,2,4},{1,5},{0,4},{3,5,1},{4,2}};
        queue<string> solutionStates;
        unordered_set<string> record;
        record.insert(init);
        solutionStates.push(init);
        int depth = 0;
        while (!solutionStates.empty()) {
            int size = solutionStates.size();
            while (size--) {
                string currentSolution = solutionStates.front();
                if (currentSolution == "123450") return depth;

                int zeroPosition = (int) currentSolution.find("0");
                for (auto nextPos : movePos[zeroPosition]) {
                    std::swap(currentSolution[zeroPosition], currentSolution[nextPos]);
                    if (record.find(currentSolution) == record.end()) {
                        record.insert(currentSolution);
                        solutionStates.push(currentSolution);
                    }
                    std::swap(currentSolution[zeroPosition], currentSolution[nextPos]);
                }
                solutionStates.pop();
            }
            depth++;
        }
        return -1;
    }
};

分析

这道题写的好累= =巨tm多东西

首先我们可以发现这道题和八数码问题很像,八数码是移动空白格,而本题是移动0所在的格。因此马上可以想到是用BFS的(当然哪位读者猜不到也没什么关系因为这题是我根据BFS的tag找到的所以我肯定是能想到BFS的)。

我们将当前board的每个状态视作BFS的一个节点,根据0所在的位置,这个节点可以发展出2个或者3个不同的下线,同样也是board的状态。因此我们经营BFS的队列(queue),注意的是要每次记录当前节点的深度depth。除此以外,由于存储为vector<vector<int>>的存储代价比较大,因此我们选择存储为string的形式,可以有效地降低内存消耗。

使用movePos记录0在每个位置时,可以发生swap的位置(在string中),进一步降低了开销。

再然后,使用了record记录了已经过BFS遍历的节点,存储起来防止多次进行重复的BFS。同时这也使得本算法可以断定某些情况没有解。

这里还有一些奇奇怪怪的优化,比如recordunordered_set,要比使用set或者其他什么数据结构要快得多;又比如movePosvector<vector<int>>要比用map<int, vector<int>>还要快一些。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值