Press "Enter" to skip to content

花花酱 LeetCode 773. Sliding Puzzle

题目大意:给你一个2×3的棋盘,放着0-5。每一步0可以和上下左右的一个数交换。问需要多少步可以构成123450的棋盘状态。

Problem:

On a 2×3 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:

Solution: BFS

Time complexity: O(6!)

Space complexity: O(6!)

C++

Simplified, only works on 3×2 board

 

 

请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.

Buy anything from Amazon to support our website
您可以通过在亚马逊上购物(任意商品)来支持我们

Paypal
Venmo
huahualeetcode
微信打赏

Be First to Comment

Leave a Reply