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 __init__(self, filename):
self.initial_state = self.load_game_state(filename)
def load_game_state(self, filename):
with open(filename,'r') as file:
lines = file.read().splitlines()
if not lines:
print("Empty input file. Please provide valid input.")
sys.exit(1)
try:
dimensions =[int(dim) for dim in lines[0].split(',') if dim.strip()]
if len(dimensions)!=2:
raise ValueError("Invalid dimensions format.")
except ValueError as e:
print(f"Error parsing dimensions from the first line: {e}")
sys.exit(1)
state_matrix =[]
for line in lines[1:]:
if line.strip():
row =[]
values = line.split(',')
for value in values:
try:
if value.strip():
row.append(int(value))
else:
print(f"Warning: Empty value in row: {line}")
except ValueError as e:
print(f"Error parsing value '{value}' in row '{line}': {e}")
sys.exit(1)
state_matrix.append(row)
return [dimensions, state_matrix]
def display_state(self, state):
dimensions, matrix = state
w, h = dimensions
print()
for i in range(h):
row =",".join(map(str, matrix[i]))
print(row)
print()
def compare_states(self, state1, state2):
return state1== state2
def normalize_state(self, state):
dimensions, matrix = state
w, h = dimensions
next_idx =3
for i in range(h):
for j in range(w):
if matrix[i][j]== next_idx:
next_idx +=1
elif matrix[i][j]> next_idx:
self.swap_idx(matrix, next_idx, matrix[i][j])
next_idx +=1
return [dimensions, matrix]
def swap_idx(self, matrix, idx1, idx2):
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if matrix[i][j]== idx1:
matrix[i][j]= idx2
elif matrix[i][j]== idx2:
matrix[i][j]= idx1
def available_moves(self, state):
dimensions, matrix = state
w, h = dimensions
moves =[]
for i in range(h):
for j in range(w):
if matrix[i][j]>=3:
self.add_possible_moves(moves, matrix[i][j], i, j, w, h)
return moves
def add_possible_moves(self, moves, piece, row, col, w, h):
if row >0:
moves.append((piece,'up'))
if row < h -1:
moves.append((piece, 'down'))
if col >0:
moves.append((piece, 'left'))
if col < w -1:
moves.append((piece, 'right'))
def apply_move(self, state, move):
dimensions, matrix = state
w, h = dimensions
piece, direction = move
for i in range(h):
for j in range(w):
if matrix[i][j]== piece:
new_i, new_j = self.get_new_position(i, j, direction)
if self.is_valid_move(matrix, piece, new_i, new_j, w, h):
matrix[i][j]=0
matrix[new_i][new_j]= piece
return self.normalize_state([dimensions, matrix])
return state
def get_new_position(self, i, j, direction):
if direction =='up':
return i -1, j
elif direction == 'down':
return i +1, j
elif direction == 'left':
return i, j -1
elif direction == 'right':
return i, j +1
def is_valid_move(self, matrix, piece, new_i, new_j, w, h):
return 0<= new_i < h and 0<= new_j < w and matrix[new_i][new_j]==0
def done(self, state):
dimensions, matrix = state
for row in matrix:
if -1 in row:
return False
return True
def random_walk(self, state, n):
moves =[]
for _ in range(n):
possible_moves = self.available_moves(state)
if not possible_moves:
break
move = possible_moves[0]
moves.append(move)
state = self.apply_move(state, move)
self.display_state(state)
if self.done(state):
break
return moves, state
def bfs(self, state):

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!