【leetcode】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].
解题思路:对于这个题目,我也没想到特别好的方法。不过既然题目约定了是一个2*3的board,那么基本上就不用考虑性能问题了,所以可以简单粗暴的用穷举法。怎么穷举呢,最简单的是倒推,因为如果题目有解的话最终的结果一定是 [[1,2,3],[4,5,0]],我们可以用这个状态作为起点,计算出多少次移动能移动到和输入board一样的状态。因为只能移动0,而且只有上下左右四种移动方向,这个就是一个很典型的广度遍历的场景,注意每次移动后都要记录当前的状态,也要记录到达这个状态需要移动的次数,用来和board比较,如果一致就不需要再继续移动了。最后,当然也可以事先把所有能移动到达的状态先计算出来并进行缓存,之后就直接和board进行比较就行了。
完整代码:
import copy
class Solution(object):
def slidingPuzzle(self, board):
"""
:type board: List[List[int]]
:rtype: int
"""
pass
def toString(self,b):
s = ''
for i in b:
for j in i:
s += str(j)
return s
def getIndex(self,b):
for i in range(len(b)):
for j in range(len(b[0])):
if b[i][j] == 0:
return [i,j]
return []
def swap(self,b,x1,y1,x2,y2):
t = b[x1][y1]
b[x1][y1] = b[x2][y2]
b[x2][y2] = t
def slidingPuzzle(self, board):
des = [[1,2,3],[4,5,0]]
#print self.toString(board)
res = []
res.append(self.toString(des))
stack = []
stack.append([des,0]) while len(stack) > 0:
e = stack[0]
if e[0] == board:
return e[1]
#break
del stack[0] #print 'step',e[1]
#print e[0][0]
#print e[0][1]
#print '**********' inx = self.getIndex(e[0])
#up:
if inx[0] - 1 == 0:
bup = copy.deepcopy(e[0])
self.swap(bup, inx[0], inx[1], inx[0] -1,inx[1])
if self.toString(bup) not in res:
res.append(self.toString(bup))
stack.append([bup,e[1]+1])
#down
if inx[0] + 1 == 1:
bdown = copy.deepcopy(e[0])
self.swap(bdown, inx[0], inx[1], inx[0] + 1,inx[1])
if self.toString(bdown) not in res:
res.append(self.toString(bdown))
stack.append([bdown,e[1]+1])
#left
if inx[1] - 1 >= 0:
bleft = copy.deepcopy(e[0])
self.swap(bleft, inx[0], inx[1], inx[0],inx[1]-1)
if self.toString(bleft) not in res:
res.append(self.toString(bleft))
stack.append([bleft,e[1]+1])
#right
if inx[1] + 1 <= 2:
bright = copy.deepcopy(e[0])
self.swap(bright, inx[0], inx[1], inx[0] ,inx[1]+1)
if self.toString(bright) not in res:
res.append(self.toString(bright))
stack.append([bright,e[1]+1])
return -1
【leetcode】Sliding Puzzle的更多相关文章
- 【LeetCode】BFS(共43题)
[101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...
- 【LeetCode】堆 heap(共31题)
链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
随机推荐
- matlab2012a过期问题解决办法(转载)
转载:http://blog.sina.com.cn/s/blog_4a46812b0102x694.html 以前安装过Matlab2013a等高版本,发现自己win7 系统每次重启后,Matl ...
- MYSQL5.5 linux安装
1.常规的编译安装MYSQL 此种方法使用所有Mysql5.0 - 5.1 系列产品 比较常规的编译方式 2. 采用cmake 方式编译安装Mysql 3.二进制安装方式 免编译安装MYSQL 4.如 ...
- tarjan缩点相关知识及代码
emmm原谅我确实是找不到不用缩点的tarjan题才会想到自学一下缩点这个东西的.. 题目没有,只能自己出数据并手动模拟... 首先看一张图(懒得画,还是看输入数据吧,劳烦自行画图..) 7 9(n个 ...
- 软件设计分为结构化设计(SD)
软件设计分为结构化设计(SD)与面向对象设计(OOD). 其中结构化设计SD是一种面向数据流的方法,它以SRS(软件需求规格说明书)和SA(结构化分析)阶段所产生的和数据字典等文档为基础,是一个自顶向 ...
- 自带的simple认证
参考: hdfs权限: 官网http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_permissions_guide.html hdfs权限: http://dyi ...
- ucloud自动创建instance
用terrform for ucloud: https://www.terraform.io/docs/providers/ucloud/index.html https://docs.ucloud. ...
- Comet OJ - Contest #13 「火鼠的皮衣 -不焦躁的内心-」
来源:Comet OJ - Contest #13 芝士相关: 复平面在信息学奥赛中的应用[雾 其实是道 sb 题??? 发现原式貌似十分可二项式定理,然后发现确实如此 我们把 \(a^i\) 替换成 ...
- Winform CheckBox组,先横向排列,后纵向排列,点击文字,改变Checkbox的状态的方法
开始选用的CheckedListBox控件,不能实现,改为使用ListView控件,可以满足需求.操作步骤如下: 1.将ListView的属性View改为SmallIcon. 2.CheckBox ...
- 082、数据收集利器 cAdvisor (2019-04-30 周二)
参考https://www.cnblogs.com/CloudMan6/p/7683190.html cAdvisor 是google 开发的容器监控工具,下面我们开始安装和体验 cAdvisor ...
- vue组件如何引入外部.js/.css/.scss文件
可在相应的单vue组件引入相应文件. 1.引入外部.js文件. 2.引入外部.css文件. 使用@import引入外部css,作用域是全局的,也可在相应的单vue组件引入,import并不是引入代码到 ...