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 ...
随机推荐
- ubuntu14.04安装hadoop2.6.0(伪分布模式)
版本:虚拟机下安装的ubuntu14.04(64位),hadoop-2.6.0 下面是hadoop2.6.0的官方英文教程: http://hadoop.apache.org/docs/r2.6.0/ ...
- IdentityServer4在Asp.Net Core中的应用(一)
IdentityServer4是一套身份授权以及访问控制的解决方案,专注于帮助使用.Net 技术的公司为现代应用程序建立标识和访问控制解决方案,包括单点登录.身份管理.授权和API安全. 下面我将具体 ...
- MYSQL题讲答案
2丶查询‘生物’课程比‘物理’课程成绩高的所有学生的学号 思路: 获取所有有生物课成的人(学号,成绩) -- 临时表 获取所有有物理课程的人(学号,成绩) -- 临时表 根据[学号]连接两个临时表: ...
- 新男人八题---AStringGame
终于完成进度男人1/8,为了这题学了sam= = 题意先有一个串,n个子串,两个人轮流每次在子串上加字符,要求加完后还是原串的子串,最后不能加的就是输者,求赢的人 解法:sam之后在构造的状态图上跑s ...
- 搞懂分布式技术3:初探分布式协调服务zookeeper
搞懂分布式技术3:初探分布式协调服务zookeeper 1.Zookeepr是什么 Zookeeper是一个典型的分布式数据一致性的解决方案,分布式应用程序可以基于它实现诸如数据发布/订阅,负载均衡, ...
- python类常用装饰器
class Myclass(object): def __init__(self): pass #必须实例化才能调用 def sayhi(self): print 'hello' #静态方法,跟类没什 ...
- Python内置函数详解-总结篇
参考链接:http://www.cnblogs.com/sesshoumaru/p/6140987.html
- Idea_02_常用配置
一.前言 在上一节,我们安装并激活了IDEA,这一节我们来设置下Idea的常用配置: 项目相关配置 Idea常用配置 二.项目相关配置 运行Idea,出现下图 1.配置默认JDK 1.1 添加 SDK ...
- php获取当前日期开始一周日期与星期几
不说了,对于PHPer来说,写不出来说什么都是白瞎,不喜勿喷~~~~ function get_week(){ $data = []; $format='Y-m-d'; for ($i=0; $i&l ...
- va_start、va_arg、va_end、va_copy 可变参函数
1.应用与原理 在C语言中,有时我们无法给出一个函数参数的列表,比如: int printf(const char *format, ...); int fprintf(FILE *s ...