Question: iu have the code and it prints but not as required please help me to get the same output from my code here is the
iu have the code and it prints but not as required please help me to get the same output from my code here is the code import sys
import time
from collections import deque
class SlidingBrickPuzzle:
def initself filename:
self.initialstate self.loadgamestatefilename
def loadgamestateself filename:
with openfilenamer as file:
lines file.readsplitlines
if not lines:
printEmpty input file. Please provide valid input."
sysexit
try:
dimensions intdim for dim in linessplit if dim.strip
if lendimensions:
raise ValueErrorInvalid dimensions format."
except ValueError as e:
printfError parsing dimensions from the first line: e
sysexit
statematrix
for line in lines::
if line.strip:
row
values line.split
for value in values:
try:
if value.strip:
row.appendintvalue
else:
printfWarning: Empty value in row: line
except ValueError as e:
printfError parsing value value in row line: e
sysexit
statematrix.appendrow
return dimensions statematrix
def displaystateself state:
dimensions, matrix state
w h dimensions
print
for i in rangeh:
row joinmapstr matrixi
printrow
print
def comparestatesself state state:
return state state
def normalizestateself state:
dimensions, matrix state
w h dimensions
nextidx
for i in rangeh:
for j in rangew:
if matrixij nextidx:
nextidx
elif matrixij nextidx:
self.swapidxmatrix nextidx, matrixij
nextidx
return dimensions matrix
def swapidxself matrix, idx idx:
for i in rangelenmatrix:
for j in rangelenmatrixi:
if matrixij idx:
matrixij idx
elif matrixij idx:
matrixij idx
def availablemovesself state:
dimensions, matrix state
w h dimensions
moves
for i in rangeh:
for j in rangew:
if matrixij:
self.addpossiblemovesmoves matrixij i j w h
return moves
def addpossiblemovesself moves, piece, row, col, w h:
if row :
moves.appendpieceup
if row h :
moves.appendpiece 'down'
if col :
moves.appendpiece 'left'
if col w :
moves.appendpiece 'right'
def applymoveself state, move:
dimensions, matrix state
w h dimensions
piece, direction move
for i in rangeh:
for j in rangew:
if matrixij piece:
newi newj self.getnewpositioni j direction
if self.isvalidmovematrix piece, newi newj w h:
matrixij
matrixnewinewj piece
return self.normalizestatedimensions matrix
return state
def getnewpositionself i j direction:
if direction up:
return i j
elif direction 'down':
return i j
elif direction 'left':
return i j
elif direction 'right':
return i j
def isvalidmoveself matrix, piece, newi newj w h:
return newi h and newj w and matrixnewinewj
def doneself state:
dimensions, matrix state
for row in matrix:
if in row:
return False
return True
def randomwalkself state, n:
moves
for in rangen:
possiblemoves self.availablemovesstate
if not possiblemoves:
break
move possiblemoves
moves.appendmove
state self.applymovestate move
self.displaystatestate
if self.donestate:
break
return moves, state
def bfsself state:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
