首页 技术 正文
技术 2022年11月19日
0 收藏 312 点赞 4,074 浏览 2621 个字

题目如下:

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
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919