Sliding Puzzle
On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square represented by 0.
A move consists of choosing 0 and a 4-directionally adjacent number and swapping it.
The state of the board is solved if and only if the board is [[1,2,3],[4,5,0]].
Given a puzzle board, return the least number of moves required so that the state of the board is solved. If it is impossible for the state of the board to be solved, return -1.
Examples:
Input: board = [[1,2,3],[4,0,5]]
Output: 1
Explanation: Swap the 0 and the 5 in one move.
Input: board = [[1,2,3],[5,4,0]]
Output: -1
Explanation: No number of moves will make the board solved.
Input: board = [[4,1,2],[5,0,3]]
Output: 5
Explanation: 5 is the smallest number of moves that solves the board.
An example path:
After move 0: [[4,1,2],[5,0,3]]
After move 1: [[4,1,2],[0,5,3]]
After move 2: [[0,1,2],[4,5,3]]
After move 3: [[1,0,2],[4,5,3]]
After move 4: [[1,2,0],[4,5,3]]
After move 5: [[1,2,3],[4,5,0]]
Input: board = [[3,2,4],[1,5,0]]
Output: 14
Note:
boardwill be a 2 x 3 array as described above.board[i][j]will be a permutation of[0, 1, 2, 3, 4, 5].
分析:这题可以用bfs解,但是刚拿到这题的时候,完全想不到这题是可以用bfs解决的。
class Solution {
public int slidingPuzzle(int[][] board) {
String target = "";
String start = "";
for (int i = ; i < board.length; i++) {
for (int j = ; j < board[].length; j++) {
start += board[i][j];
}
}
HashSet<String> visited = new HashSet<>();
// all the positions 0 can be swapped to
int[][] dirs = new int[][] { { , }, { , , },
{ , }, { , }, { , , }, { , } };
Queue<String> queue = new LinkedList<>();
queue.offer(start);
visited.add(start);
int res = ;
while (!queue.isEmpty()) {
// level count, has to use size control here, otherwise not needed
int size = queue.size();
for (int i = ; i < size; i++) {
String cur = queue.poll();
if (cur.equals(target)) {
return res;
}
int zero = cur.indexOf('');
// swap if possible
for (int dir : dirs[zero]) {
String next = swap(cur, zero, dir);
if (visited.contains(next)) {
continue;
}
visited.add(next);
queue.offer(next);
}
}
res++;
}
return -;
}
private String swap(String str, int i, int j) {
StringBuilder sb = new StringBuilder(str);
sb.setCharAt(i, str.charAt(j));
sb.setCharAt(j, str.charAt(i));
return sb.toString();
}
}
Sliding Puzzle的更多相关文章
- leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings
542. 01 Matrix https://www.cnblogs.com/grandyang/p/6602288.html 将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值. 最 ...
- Leetcode之广度优先搜索(BFS)专题-773. 滑动谜题(Sliding Puzzle)
Leetcode之广度优先搜索(BFS)专题-773. 滑动谜题(Sliding Puzzle) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...
- [Swift]LeetCode773. 滑动谜题 | Sliding Puzzle
On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square repre ...
- [LeetCode] Sliding Puzzle 滑动拼图
On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square repre ...
- LeetCode 773. Sliding Puzzle
原题链接在这里:https://leetcode.com/problems/sliding-puzzle/description/ 题目: On a 2x3 board, there are 5 ti ...
- [LeetCode] 773. Sliding Puzzle 滑动拼图
On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square repre ...
- 【leetcode】Sliding Puzzle
题目如下: On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square ...
- 【LeetCode】773. Sliding Puzzle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/sliding- ...
- 滑动拼图 Sliding Puzzle
2018-09-09 22:01:02 问题描述: 问题求解: 问题很Interesting,其实本质就是解空间遍历,使用BFS就可以很快的予以解决~ public int slidingPuzzle ...
随机推荐
- SSH 中文乱码解决
在终端执行命令:export LC_ALL=zh_CN.GB2312;export LANG=zh_CN.GB2312是最有效的. 这种方法是临时的,只对当前SSH客户端有效,重启后依然乱码. 1.不 ...
- Linux进程通信之文件
父子进程共享打开的文件描述符------使用文件完成进程间通信. /*** fork_share_fd.c ***/ #include <stdio.h> #include <uni ...
- Appium环境搭建(win/mac)
课程使用Windows+Android虚拟机, 建议使用Windows系统学习课程, 如使用Mac系统, 请另外准备一台Andorid手机 Windows系统Appium环境搭建 安装JDK并配置环境 ...
- python 绘制f(x)=x^2
code import turtle import math turtle.speed() # 画一个平方的函数 turtle.penup() turtle., *) turtle.pendown() ...
- [JZOJ6346]:ZYB和售货机(拓扑+基环内向森林)
题目描述 可爱的$ZYB$来到一个售货机前. 售货机里有一共有$N(\leqslant 10^5)$个物品,每个物品有$A_i$个.自然,还有$N$个购买按钮.正常情况下,按下第$i$个按钮,需要支付 ...
- ARTS打卡计划第九周
Algorithms: https://leetcode-cn.com/problems/merge-two-sorted-lists/submissions/ 合并两个链表 Review: “Pu ...
- SchedulerFactoryBean的问题
http://blog.csdn.net/beliefer/article/details/51578546
- GDB之常见错误
1. dl-debug.c:74: No such file or directory. 如上图,当使用 gdb 调试执行 r 时,会出现如上打印. 解决方法如下: apt-get install e ...
- Beta冲刺(2/5)
队名:new game 组长博客 作业博客 组员情况 鲍子涵(队长) 过去两天完成了哪些任务 验收游戏素材 学习Unity 2D Animation系统 基本做完了人物的各个动画 接下来的计划 冲击E ...
- JavaScript之屏幕上下左右滑动监听
前言 存在这么一个需求,根据用户在屏幕不同的滑动方向(上.下.左.右),使用js脚本判断出不同的滑动行为,更新网页为不同的界面. 源码 参考了博文[1]的源码,但由于存在一些漏洞,比如:上下滑动事件监 ...