Leetcode之广度优先搜索(BFS)专题-773. 滑动谜题(Sliding Puzzle)

BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree Level Order Traversal)

相同思路的题目:Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock)

相同思路的题目2:Leetcode之广度优先搜索(BFS)专题-127. 单词接龙(Word Ladder)


在一个 2 x 3 的板上(board)有 5 块砖瓦,用数字 1~5 来表示, 以及一块空缺用 0 来表示.

一次移动定义为选择 0 与一个相邻的数字(上下左右)进行交换.

最终当板 board 的结果是 [[1,2,3],[4,5,0]] 谜板被解开。

给出一个谜板的初始状态,返回最少可以通过多少次移动解开谜板,如果不能解开谜板,则返回 -1 。

示例:

输入:board = [[1,2,3],[4,0,5]]
输出:1
解释:交换 0 和 5 ,1 步完成
输入:board = [[1,2,3],[5,4,0]]
输出:-1
解释:没有办法完成谜板
输入:board = [[4,1,2],[5,0,3]]
输出:5
解释:
最少完成谜板的最少移动次数是 5 ,
一种移动路径:
尚未移动: [[4,1,2],[5,0,3]]
移动 1 次: [[4,1,2],[0,5,3]]
移动 2 次: [[0,1,2],[4,5,3]]
移动 3 次: [[1,0,2],[4,5,3]]
移动 4 次: [[1,2,0],[4,5,3]]
移动 5 次: [[1,2,3],[4,5,0]]
输入:board = [[3,2,4],[1,5,0]]
输出:14

提示:

  • board 是一个如上所述的 2 x 3 的数组.
  • board[i][j] 是一个 [0, 1, 2, 3, 4, 5] 的排列.

0每次只能和他的上下左右交换,利用一个vis数组,参考相似题目思路,求解题目。

相同思路的题目:Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock)

相同思路的题目2:Leetcode之广度优先搜索(BFS)专题-127. 单词接龙(Word Ladder)

本题AC代码:

class Solution {
int[] dirx = {1, -1, 0, 0};
int[] diry = {0, 0, 1, -1}; private static class POINT {
int x, y;
int step;
int[][] board; POINT(int x, int y, int step, int[][] board) {
this.x = x;
this.y = y;
this.step = step;
this.board = board;
}
} public int slidingPuzzle(int[][] board) {
int m = 2;
int n = 3;
Set<String> vis = new HashSet<>();
Queue<POINT> queue = new LinkedList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 0) {
vis.add(board[0][0] + " " + board[0][1] + " " + board[0][2] + " " + board[1][0] + " " + board[1][1] + " " + board[1][2]);
queue.offer(new POINT(i, j, 0, board)); }
}
}
while (!queue.isEmpty()) {
POINT temp = queue.poll();
int x = temp.x;
int y = temp.y;
int step = temp.step;
int[][] map = temp.board;
if (map[0][0] == 1 && map[0][1] == 2 && map[0][2] == 3 && map[1][0] == 4 && map[1][1] == 5 && map[1][2] == 0) {
return step;
}
for (int i = 0; i < 4; i++) {
int[][] cloneMap = new int[m][n];
for (int a = 0; a < m; a++) {
System.arraycopy(map[a], 0, cloneMap[a], 0, n);
}
int xx = x + dirx[i];
int yy = y + diry[i];
if (xx >= 0 && yy >= 0 && xx < m && yy < n) {
cloneMap[x][y] = cloneMap[xx][yy];
cloneMap[xx][yy] = 0;
String str = cloneMap[0][0] + " " + cloneMap[0][1] + " " + cloneMap[0][2] + " " + cloneMap[1][0] + " " + cloneMap[1][1] + " " + cloneMap[1][2];
if (!vis.contains(str)) {
vis.add(str);
queue.offer(new POINT(xx, yy, step + 1, cloneMap));
}
}
}
} return -1;
}
}

Leetcode之广度优先搜索(BFS)专题-773. 滑动谜题(Sliding Puzzle)的更多相关文章

  1. [Swift]LeetCode773. 滑动谜题 | Sliding Puzzle

    On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square repre ...

  2. Leetcode之广度优先搜索(BFS)专题-详解429. N叉树的层序遍历(N-ary Tree Level Order Traversal)

    Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree Level Order Traversal) 给定一个 N 叉树,返回其节点值的层序遍历. (即从左到右 ...

  3. Leetcode之广度优先搜索(BFS)专题-127. 单词接龙(Word Ladder)

    Leetcode之广度优先搜索(BFS)专题-127. 单词接龙(Word Ladder) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tre ...

  4. Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock)

    Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...

  5. Leetcode之广度优先搜索(BFS)专题-1162. 地图分析(As Far from Land as Possible)

    Leetcode之广度优先搜索(BFS)专题-1162. 地图分析(As Far from Land as Possible) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. ...

  6. Leetcode之广度优先搜索(BFS)专题-994. 腐烂的橘子(Rotting Oranges)

    Leetcode之广度优先搜索(BFS)专题-994. 腐烂的橘子(Rotting Oranges) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ar ...

  7. Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares)

    Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ar ...

  8. Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)

    Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...

  9. Leetcode之广度优先搜索(BFS)专题-529. 扫雷游戏(Minesweeper)

    Leetcode之广度优先搜索(BFS)专题-529. 扫雷游戏(Minesweeper) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tre ...

随机推荐

  1. python的序列化模块

    最近机器学习的模型需要序列化和反序列化,因为写个博客总结一下几个模型和数据等序列化的模块.

  2. 一例jsonp跨域访问

    对于网站A,有一链接 '/auth/list',返回json数据 {, , , , , },{, , , , , }]} 网站b某页面下可以这样写jsonp get请求 <script> ...

  3. RAID的多种实现

    RAID的诞生 由加利福尼亚大学伯克利分校(University of California-Berkeley)在1988年,发表的文章:"A Case for Redundant Arra ...

  4. 题解 比赛 match

    比赛 match Description 有 N 支队伍打比赛.已知有如下条件: • 每支队伍恰好打了 4 场比赛 • 对于一场比赛,如果是平局,双方各得 1 分:否则胜者得 3 分,负者不得分 给定 ...

  5. Java 实现在固定区间内随机生成整数

    1.random.nextInt(num) public static void main(String args[]) { Random rdom = new Random(); int max = ...

  6. ListView如何添加数据如何不闪烁

    public class DoubleBufferListView : ListView        {            public DoubleBufferListView()       ...

  7. learning armbian steps(10) ----- armbian 源码分析(五)

    在上一节的分析当中,已经知道了uboot kernel的源代码路径及编译选项,以及rootfs的版本,相关信息如下所示: ## BUILD CONFIGURATION Build target: Bo ...

  8. Git 如何针对项目修改本地提交提交人的信息

    Git 如果不进行修改的话,在默认情况下将会使用全局的用户名称和电子邮件. 但是在 GitHub 中是通过用户邮件来进行提交人匹配的. 如何针对项目来修改提交的用户信息? 针对 TortoiseGit ...

  9. poj 3190 贪心+优先队列优化

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4274   Accepted: 153 ...

  10. ArrayList,LinkedList和String

    import java.util.ArrayList; public class Demo{ public static void main(String[] args) throws Excepti ...