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. 近期总结的一些Java基础

    1.面向过程:当需要实现一个功能的时候,每一个过程中的详细步骤和细节都要亲力亲为. 2.面向对象:当需要实现一个功能的时候,不关心详细的步骤细节,而是找人帮我做事. 3.类和对象的关系:   a-类是 ...

  2. 整型和浮点型与QByteArray的转换

    目录 QByteArray 整型 QByteArray 浮点型 QByteArray QByteArray The QByteArray class provides an array of byte ...

  3. Azure AD(二)调用受Microsoft 标识平台保护的 ASP.NET Core Web API 下

    一,引言 上一节讲到如何在我们的项目中集成Azure AD 保护我们的API资源,以及在项目中集成Swagger,并且如何把Swagger作为一个客户端进行认证和授权去访问我们的WebApi资源的?本 ...

  4. LiteAI四大技术"杀手锏",解锁物联网智能设备AI开发难关

    你知道我们生活中常见的物联网智能设备融合AI技术后,会给我们带来什么样的智能交互体验?在我们指尖触碰的那一刹那背后隐藏的代码世界又是怎么样的呢? 今天就来和大家说说IoT智能设备轻松实现AI的奥秘! ...

  5. masonry中的make,remake,update

    - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBar.translucent = NO; ...

  6. My sql的知识点 不足点请指点谢谢

    My SQL:(关系数据库) 数据库能够能够干吗? 1. :存储大量的信息,方便检索和访问    2. :保持数据信息的一致,完整      3. : 共享和安全 4. : 通过组合分析,产生新的有用 ...

  7. Postman学习之Postman简介

    前言:对于测试人员来说,接口测试是必须掌握的一个技能:在工作中掌握了接口自动化测试无疑是如虎添翼,那么怎么开展接口测试呢?下面将介绍一款接口测试的神器——postman 1.postman背景介绍 p ...

  8. css中文字两端对齐兼容IE

    text-align: justify; text-justify:inter-ideograph;

  9. hdu5687 字典树

    hdu5687 #include <bits/stdc++.h> using namespace std; , n = ; struct node { ]; int cnt; void i ...

  10. DPDK开发环境搭建(学会了步骤适合各版本)

    一.版本的选择 首先要说明的是,对于生产来说DPDK版本不是越高越好,如何选择合适的版本? 1.要选择长期支持的版本LTS(Long Term Support) 2.根据当前开发的系统环境选择 可以在 ...