Skip to content

Commit a4447f5

Browse files
committed
Runtime: 93 ms (Top 39.62%) | Memory: 14.1 MB (Top 16.60%)
1 parent dfed104 commit a4447f5

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

scripts/algorithms/S/Sliding Puzzle/Sliding Puzzle.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Runtime: 93 ms (Top 39.62%) | Memory: 14.1 MB (Top 16.60%)
2+
13
class Solution:
24
def slidingPuzzle(self, board: List[List[int]]) -> int:
35
def findNei(board):
@@ -13,15 +15,15 @@ def findNei(board):
1315
tmp[r][c], tmp[i][j] = tmp[i][j], tmp[r][c]
1416
boards.append(tmp)
1517
return boards
16-
18+
1719
visited = set()
1820
target = [[1, 2, 3], [4, 5, 0]]
1921
if board == target:
2022
return 0
2123
rows, cols = len(board), len(board[0])
2224
q = collections.deque()
2325
step = 1
24-
26+
2527
for row in range(rows):
2628
for col in range(cols):
2729
if board[row][col] == 0:
@@ -32,7 +34,7 @@ def findNei(board):
3234
visited.add(tuple([tuple(row) for row in b]))
3335
q.append(b)
3436
break
35-
37+
3638
while q:
3739
step += 1
3840
for _ in range(len(q)):
@@ -45,5 +47,5 @@ def findNei(board):
4547
if t not in visited:
4648
visited.add(t)
4749
q.append(b)
48-
49-
return -1
50+
51+
return -1

0 commit comments

Comments
 (0)