原题链接在这里: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:

  • board will 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的更多相关文章

  1. [LeetCode] 773. Sliding Puzzle 滑动拼图

    On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square repre ...

  2. 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的值. 最 ...

  3. 【LeetCode】773. Sliding Puzzle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/sliding- ...

  4. 【leetcode】Sliding Puzzle

    题目如下: On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square ...

  5. Leetcode之广度优先搜索(BFS)专题-773. 滑动谜题(Sliding Puzzle)

    Leetcode之广度优先搜索(BFS)专题-773. 滑动谜题(Sliding Puzzle) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...

  6. [LeetCode] Sliding Puzzle 滑动拼图

    On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square repre ...

  7. [Swift]LeetCode773. 滑动谜题 | Sliding Puzzle

    On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square repre ...

  8. LeetCode 480. Sliding Window Median

    原题链接在这里:https://leetcode.com/problems/sliding-window-median/?tab=Description 题目: Median is the middl ...

  9. [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 ...

随机推荐

  1. 关于http请求ContentType:application/x-www-form-urlencoded

    在又一次http请求过程中,模拟post请求提交form表单数据一直提示部分参数为空,后面检查发现是缺少ContentType:application/x-www-form-urlencoded的原因 ...

  2. 定时模块app_timer用法及常见问题—nRF5 SDK模块系列二

    app_timer是大家经常用到的一个库,app_timer的功能就是定时,也就是说,你在某一时刻启动一个app timer并设定超时时间,超时时间一到,app_timer就会回调timeout ha ...

  3. LeetCode第[98]题(Java):Validate Binary Search Tree(验证二叉搜索树)

    题目:验证二叉搜索树 难度:Medium 题目内容: Given a binary tree, determine if it is a valid binary search tree (BST). ...

  4. 设计模式--模板方法模式C++实现

    模板方法模式C++实现 1定义 定义一个操作的算法的框架,而将一些步骤延迟到子类中.使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 实现方案:将算法/逻辑框架放在抽象基类中,并定义好 ...

  5. Linux命令详解-date

    在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式:   date [ ...

  6. flask学习(五):使用配置文件

    1. 新建一个config.py文件 2. 在主app文件中导入这个文件,并且配置到app中,示例代码如下: import config app.config.from_object(config) ...

  7. How to create Oracle ASM devices using device-mapper multipath devices in Red Hat Enterprise Linux 6

    How to create Oracle ASM devices using device-mapper multipath devices in Red Hat Enterprise Linux 6 ...

  8. laravel中上传图片之后图片的处理

    $file=Input::file('file'); if ($file->isValid()){ $entension=$file->getClientOriginalExtension ...

  9. 关于ie下消除offset 问题解决

    这个问题应该是上周就发现了,所有页面都没有问题,偏偏就一个页面出现不兼容问题,而且他们用的是同一个文件.这真是一个神奇的事情.今天一来上班,我就着手解决这个问题,分析查看原来是这个元素多了一个offs ...

  10. 网页重构中区分IE6、IE7、IE8及标准浏览器的最佳方法

    由于万恶的IE6和IE7,我们在页面重构时不免要对其进行各种bug修复及差异化处理.在标准浏览器中可实现的效果在IE里却有各种离奇问题,例如IE6.IE7不能良好应对的inline-block和.cl ...