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 ...
随机推荐
- Kafka 的一些知识点整理【1】
First: Kafka 是什么? Kafka 是一个发布订阅系统 最初是是LinkedIn 开发 最后交给Apache 开源组织 github地址:https://github.com/apache ...
- 关于oracle怎么看清楚字段的一些实践
在oracle存储过程或者平时编码中会有很多时候对不上字段,这时候在字段逗号后面可以主动加上--数字. 还有的是应该注意尽量让每个字段都占有一行的空间.下面部分截图说明
- 豹子安全-注入工具-显错注入-asp_POST_显错_SQLServer_GetWebShell
豹子安全-注入工具-显错注入-SQL Server-上传WebShell 请看下列视频 . 该视频在停留10秒钟后开始. 或点击此处查看:豹子安全-注入工具-asp_POST_显错_SQLServer ...
- 去除 HTML 和 PHP 标记
strip_tags()函数,从字符串中去除html和php标记,随笔记一下,如果看过就过去,下次再找可能又会费点事,记下来下次好找
- 我的linux学习日记day5
一.vim 编辑器 有三种模式,命令模式,输入模式,末行模式 1.下面是命令模式常用的命令 2.末行模式常用命令 :w 保存 :q 退出 :q! 强制退出 :wq! 强制保存退出 :set nu 显示 ...
- sqlite聚合函数
常见聚合函数 avg(X) 用于返回组中所有非空列的平均值.字符串(string)或二进制数据(BLOB)等非数字类型当作0来计算.结果是浮点型的数据,即便所有数据中只有一个整数(integer)的数 ...
- B. Sleepy Game 博弈搜索
题意:给一个有向图和起点,然后只有一名选手,这名选手可以随意挪动棋子,最终不能动的时候走过的边为奇数边为Win并输出路径,否则如果有环输出Draw,否则输出Lose; 题目链接 知道状态数最多只有n* ...
- poj3764字典树路径最大异或和
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6853 Accepted: 1 ...
- 四、HTML属性—— HTML 元素提供的附加信息
HTML属性 (1)属性一般描述于开始标签 (2)属性总是以名称/值对的形式出现,比如:name="value" (3)使用小写属性 HTML属性值 应该始终被包括在引号内. —— ...
- 28-2 类型转换函数Cast-Convet
------------------------类型转换函数------------------------ --cast(表达式 as 数据类型) --convert(数据类型,表达式) ' as ...