LeetCode 773. Sliding Puzzle
原题链接在这里:https://leetcode.com/problems/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].
题解:
题目说道least number of moves, 应该想到用BFS.
Queue里面放上现在board的状态, 利用新的类Node, 记录board, 0所在位置和查询的深度.
poll时如果出现了target状态,就找到了结果, 返回深度.
BFS用Set来保存出现过的状态. Serialize the board to string.
Time Complexity: O((m*n)!*m*n). m = board.length, n = board[0].length. board一共有(m*n)! 种可能状态, 也就是queue的可能长度. 处理queue的每个node用时O(m*n).
Space: O((m*n)!).
AC Java:
class Solution {
public int slidingPuzzle(int[][] board) {
int m = 2;
int n = 3;
String target = "123450";
String start = "";
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
start = start + board[i][j];
}
}
HashSet<String> visited = new HashSet<>();
visited.add(start);
LinkedList<String> que = new LinkedList<>();
que.add(start);
int level = 0;
int [][] dirs = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
while(!que.isEmpty()){
int size = que.size();
while(size-- > 0){
String cur = que.poll();
if(cur.equals(target)){
return level;
}
int ind = cur.indexOf('0');
int r = ind / n;
int c = ind % n;
for(int [] dir : dirs){
int x = r + dir[0];
int y = c + dir[1];
if(x < 0 || x >= m || y < 0 || y >= n){
continue;
}
int ind1 = x * n + y;
StringBuilder sb = new StringBuilder(cur);
sb.setCharAt(ind, cur.charAt(ind1));
sb.setCharAt(ind1, cur.charAt(ind));
String can = new String(sb);
if(visited.contains(can)){
continue;
}
visited.add(can);
que.add(can);
}
}
level++;
}
return -1;
}
}
LeetCode 773. Sliding Puzzle的更多相关文章
- [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 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】773. Sliding Puzzle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/sliding- ...
- 【leetcode】Sliding Puzzle
题目如下: On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square ...
- Leetcode之广度优先搜索(BFS)专题-773. 滑动谜题(Sliding Puzzle)
Leetcode之广度优先搜索(BFS)专题-773. 滑动谜题(Sliding Puzzle) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...
- [LeetCode] Sliding Puzzle 滑动拼图
On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square repre ...
- [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 480. Sliding Window Median
原题链接在这里:https://leetcode.com/problems/sliding-window-median/?tab=Description 题目: Median is the middl ...
- [LeetCode] 239. Sliding Window Maximum 滑动窗口最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
随机推荐
- JS书籍推荐
JS书籍推荐 一.总结 一句话总结: 二.JS进阶书籍 第一阶段:<JavaScript DOM编程艺术> 看这本书之前,请先确认您对Javascript有个基本的了解,应该知道if el ...
- Oracle 冷备份详解【实战案例】
Oracle 冷备份详解 --准备工作 select * from v$database; select file_name from dba_data_files; create tablespac ...
- Android_布局属性大全
RelativeLayout 第一类:属性值为true可false android:layout_centerHrizontal 水平居中 android:layout_centerVe ...
- SpringBoot Mybatis PageHelper插件报错
SpringBoot2.0.0 MyBatis1.3.2 PageHelper1.1.2插件,但是在启动运行时,抛错:org.springframework.beans.factory.BeanCre ...
- tooltip提示框组件
Tooltip 提示框组件 可独立于其他组件通过$.fn.tooltip.defaults重写默认的defaults.当用户移动鼠标指针在某个元素上时,出现提示信息窗口来显示额外信息.提示内容可以包含 ...
- jmeter测试FTP
1.下载并运行FTP服务器软件:Quick Easy FTP Server V4.0.0.exe 2.点击右上角的绿色按钮,开启服务器,直到中间的红色按钮亮起 3.在账户管理处可以管理账号信息(用户名 ...
- igmpproxy源代码学习——igmpProxyInit()
igmpproxy源代码学习--igmpProxyInit()函数详解,igmpproxy初始化 在运行igmpproxy的主程序igmpproxyRun()之前需要对igmpproxy进行一些配置, ...
- LINUX系统下的shell命令---grep、sed、awk
1)grep文本过滤命令 1.grep基本认识 (Global search regular expression and print out the line全局搜索研究正则表达时并显示出 ...
- Linux输入输出管理
一.系统输入输出的理解 运行一个程序时,需要从某个位置读取输入信息,然后CPU处理,最后将输出 显示在屏幕或文件中:其中,某个位置相当于输入设备,屏幕或文件为输出设备. 标准输入:stdin,默认 ...
- vue.js 源代码学习笔记 ----- observe
参考 vue 2.2.6版本 /* @flow */ //引入订阅者模式 import Dep from './dep' import { arrayMethods } from './array' ...