Java实现 LeetCode 773 滑动谜题(BFS)
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)的更多相关文章
- Leetcode之广度优先搜索(BFS)专题-773. 滑动谜题(Sliding Puzzle)
Leetcode之广度优先搜索(BFS)专题-773. 滑动谜题(Sliding Puzzle) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...
- Java实现 LeetCode 480 滑动窗口中位数
480. 滑动窗口中位数 中位数是有序序列最中间的那个数.如果序列的大小是偶数,则没有最中间的数:此时中位数是最中间的两个数的平均数. 例如: [2,3,4],中位数是 3 [2,3],中位数是 (2 ...
- Java实现 LeetCode 239 滑动窗口最大值
239. 滑动窗口最大值 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口每次只向右移动一位. 返回滑动窗口中的最 ...
- 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 ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 【matlab 基础篇 03】一文带你全面了解 plot 绘图函数的使用(超详细+图文并茂)
快速入门matlab,系统地整理一遍,如何你和我一样是一个新手,那么此文很适合你: 文章目录 1 前言 2 plot 2.1 显示正弦波 2.2 修改颜色 2.3 修改点的形状 2.4 修改线的形状 ...
- 自动化测试工具-Selenium IDE 教程一
引言:这里介绍的是谷歌浏览种的插件,安装教程这里不再描述,网上有很多, 使用教程不是特别多,所以特地花时间整理此篇内容: 一:打开插件,欢迎界面 启动IDE后,将显示一个欢迎对话框. 如果这是您第一次 ...
- 一分钟掌握MySQL的InnoDB引擎B+树索引
MySQL的InnoDB索引结构采用B+树,B+树什么概念呢,二叉树大家都知道,我们都清楚随着叶子结点的不断增加,二叉树的高度不断增加,查找某一个节点耗时就会增加,性能就会不断降低,B+树就是解决这个 ...
- linux下获取软件源码包 centos/redhat, debian/ubuntu
linux下获取软件源码包 centos/redhat, debian/ubuntu centos下: 1. yum install yum-utils 主要为了获取yumdownloader 2. ...
- 最简单的git 用法
步骤 在机器上ssh-keygen 然后把id_rsa.pub 复制到csdn 的公钥上去. git clone git@code.csdn.net:aca_jingru/zabbix.git 如果这 ...
- intellij tomcat
VMOption -server -XX:PermSize=128M -XX:MaxPermSize=256m
- ThreadLocal必知必会
前言 自从被各大互联网公司的"造火箭"级面试难度吊打之后,痛定思痛,遂收拾心神,从基础的知识点开始展开地毯式学习.每一个非天才程序猿都有一个对35岁的恐惧,而消除恐惧最好的方式就是 ...
- HDU3117
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3117 题目大意:对于给定的一个数 n ,求斐波那契数F(n).对于超过八位的数,给出首末四位即可. 解 ...
- vue2.0 axios前后端数据处理
目前主流的 Vue 项目,都选择 axios 来完成 ajax 请求,而大型项目都会使用 Vuex 来管理数据. 前言: 使用 cnpm 安装 axios cnpm install axios -S ...
- ImportError: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found
问题背景描述: 在做图片验证码识别安装 tensorflow 启动程序报错: ImportError: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' no ...