773. 滑动谜题

在一个 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] 的排列.

PS:

BFS搜索,交换,然后每一次都看是不是匹配

class Solution {
private static int ROW = 2;
private static int COL = 3;
private static final String RESULT = "123450";
Set<String> set;
LinkedList<String> queue; public int slidingPuzzle(int[][] board) {
if ((board.length != ROW) || (board[0].length != COL)) {
return 0;
}
set = new HashSet<>();
int time = 0;
char[] boardStr = getCharFromBoard(board);
if (RESULT.equals(Arrays.toString(boardStr))) {
return 0;
}
queue = new LinkedList<>();
queue.addLast(new String(boardStr));
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
String node = queue.pollFirst();
if (RESULT.equals(node)) {
return time;
}
boardStr = node.toCharArray();
if (boardStr[0] == '0') {
move(boardStr, 0, 1);
move(boardStr, 0, 3);
} else if (boardStr[1] == '0') {
move(boardStr, 1, 0);
move(boardStr, 1, 2);
move(boardStr, 1, 4);
} else if (boardStr[2] == '0') {
move(boardStr, 2, 1);
move(boardStr, 2, 5);
} else if (boardStr[3] == '0') {
move(boardStr, 3, 0);
move(boardStr, 3, 4);
} else if (boardStr[4] == '0') {
move(boardStr, 4, 1);
move(boardStr, 4, 3);
move(boardStr, 4, 5);
} else if (boardStr[5] == '0') {
move(boardStr, 5, 2);
move(boardStr, 5, 4);
}
}
time++;
}
return -1;
} private void move(char[] string, int i, int j) {
swap(string, i, j);
String str = new String(string);
if (!set.contains(str)) {
queue.addLast(str);
set.add(str);
}
swap(string, i, j);
} private void swap (char[] string, int i, int j) {
char temp = string[i];
string[i] = string[j];
string[j] = temp;
} private char[] getCharFromBoard(int[][] board) {
char[] res = new char[6];
res[0] = (char) ('0' + board[0][0]);
res[1] = (char) ('0' + board[0][1]);
res[2] = (char) ('0' + board[0][2]);
res[3] = (char) ('0' + board[1][0]);
res[4] = (char) ('0' + board[1][1]);
res[5] = (char) ('0' + board[1][2]);
return res;
}
}

Java实现 LeetCode 773 滑动谜题(BFS)的更多相关文章

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

    Leetcode之广度优先搜索(BFS)专题-773. 滑动谜题(Sliding Puzzle) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...

  2. Java实现 LeetCode 480 滑动窗口中位数

    480. 滑动窗口中位数 中位数是有序序列最中间的那个数.如果序列的大小是偶数,则没有最中间的数:此时中位数是最中间的两个数的平均数. 例如: [2,3,4],中位数是 3 [2,3],中位数是 (2 ...

  3. Java实现 LeetCode 239 滑动窗口最大值

    239. 滑动窗口最大值 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口每次只向右移动一位. 返回滑动窗口中的最 ...

  4. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  9. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. GroupJoin()各参数的意义及用法

    EF Core中GroupJoin的注释比较复杂: public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey ...

  2. 设计模式之GOF23状态模式

    状态模式state 场景:当具有许多状态并且需要频繁改变时,用这种模式 -电梯的运行:维修,正常,自动关门,自动开门,向上运行,向下运行,消防状态 -红绿灯:红灯,黄灯,绿灯 -企业或政府系统:公文的 ...

  3. spring mvc --自定义converse

    在MVC中我们可以很轻松的根据项目需求进行必要的信息转换,如设置默认的日期格式,自定义String类型的格式等等... 配置中我们需要自定义converseService: <bean id=& ...

  4. 学习笔记:平衡树-splay

    嗯好的今天我们来谈谈cosplay splay是一种操作,是一种调整二叉排序树的操作,但是它并不会时时刻刻保持一个平衡,因为它会根据每一次操作把需要操作的点旋转到根节点上 所谓二叉排序树,就是满足对树 ...

  5. luoguP3121解题报告

    p3121 本题首先利用一个手写栈,使元素可以快速出栈,再利用栈快速查询上一个元素在trie中的位置.

  6. [Asp.Net Core] Blazor WebAssembly - 工程向 - 如何在欢迎页面里, 预先加载wasm所需的文件

    前言, Blazor Assembly 需要最少 1.9M 的下载量.  ( Blazor WebAssembly 船新项目下载量测试 , 仅供参考. ) 随着程序越来越复杂, 引用的东西越来越多,  ...

  7. linux DRM/KMS 测试工具 modetest、kmscude、igt-gpu-tools (一)

    这里整理几个在学习Linux DRM/KMS中用到的几个工具,modetest.kmscude.igt-gpu-tools. 简介: modetest 是由libdrm提供的测试程序,可以查询显示设备 ...

  8. 计算两点间的距离(hdu2001)

    注意:在C语言中,double->lf,结果保留两位小数->0.2lf #include<stdio.h> #include<math.h> using names ...

  9. 好用的python性能测试神器–Locust

    原文链接:https://mp.weixin.qq.com/s/9PxSPuHmucSLi_welq6uNQ 现在性能测试工具太多,根据业务不同使用,比如说我们熟悉的loadrunner.jmeter ...

  10. 【github龟速克星】如何下载快如闪电

    详见:https://www.kesci.com/home/project/5e96fe1ae7ec38002d03cd56 借助第三方网站:https://g.widora.cn/