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

最新文章

  1. System.Configuration引用后ConfigurationManager方法用不了
  2. Linux安装MySql.Data for mono
  3. [转载] 深入理解Android之Java虚拟机Dalvik
  4. SQL Server调优系列基础篇(并行运算总结)
  5. ArcGIS AddIN开发异常之--修饰符“static”对该项无效
  6. HTML5小游戏【是男人就下一百层】UI美化版
  7. 【转】Fresco之强大之余的痛楚
  8. python错误收集
  9. c#生成注册码的两种方法(mac地址与IP地址)
  10. python 学习笔记 基础
  11. 设计模式之观察者模式(Java)
  12. 适用于kali linux的远程桌面开启方法(从windows xp 远程登录到kali linux )
  13. (简单) POJ 2502 Subway,Dijkstra。
  14. mybatis防止sql注入
  15. Perl一行式:处理行号和单词数
  16. Java基础知识点(三)
  17. ionic3 启动白屏处理
  18. ==、===和Object.is()的区别
  19. 如何用移动硬盘安装win7 系统
  20. Android adb shell data目录,Permission denied

热门文章

  1. mysql 查看表信息
  2. jQuery 中$.ajax()方法参数详解
  3. 第八天 RHEL7.2 文件权限管理(第一部分)
  4. HDU 4283 You Are the One ★(进出栈的括号匹配性质:区间DP)
  5. Date类型
  6. Linux中awk后面的RS, ORS, FS, OFS 用法
  7. 验证email是否合法
  8. RabbitMQ(6) 集群部署
  9. ASP.NET MVC 中使用用户控件——转
  10. 浅析Postgres中的并发控制(Concurrency Control)与事务特性(上)(转)