-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslidingPuzzle.py
More file actions
44 lines (42 loc) · 1.36 KB
/
slidingPuzzle.py
File metadata and controls
44 lines (42 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/sliding-puzzle/
# Author: Miao Zhang
# Date: 2021-03-09
class Solution:
def slidingPuzzle(self, board: List[List[int]]) -> int:
m = len(board)
n = len(board[0])
start = list()
goal = list()
for i in range(m):
for j in range(n):
start.append(board[i][j])
goal.append((i * n + j + 1) % (m * n)) #123450
if start == goal: return 0
dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]
visited = set()
visited.add(tuple(start))
steps = 0
q = collections.deque()
q.append(start)
while q:
steps += 1
size = len(q)
for _ in range(size):
s = q.popleft()
p = s.index(0)
x = p // n
y = p % n
for d in dirs:
nx = x + d[0]
ny = y + d[1]
if nx < 0 or nx >= m or ny < 0 or ny >= n: continue
pp = nx * n + ny
t = s[:]
t[p], t[pp] = t[pp], t[p]
if tuple(t) in visited: continue
if t == goal: return steps
visited.add(tuple(t))
q.append(t)
return -1