0% found this document useful (0 votes)
15 views3 pages

8 Puzzel

This document provides a Python implementation of the 8-Puzzle problem using the A* search algorithm. It includes functions to find the blank space, calculate the Manhattan distance heuristic, generate possible moves, and check if the current state is the goal state. An example usage is provided to demonstrate how to solve the puzzle from a given start state to the goal state.

Uploaded by

priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

8 Puzzel

This document provides a Python implementation of the 8-Puzzle problem using the A* search algorithm. It includes functions to find the blank space, calculate the Manhattan distance heuristic, generate possible moves, and check if the current state is the goal state. An example usage is provided to demonstrate how to solve the puzzle from a given start state to the goal state.

Uploaded by

priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Write a Program to Implement 8-Puzzle problem using Python

import heapq

# Helper function to find the position of the blank space (0)


def find_blank(state):
for r in range(3):
for c in range(3):
if state[r][c] == 0:
return r, c

# Calculate Manhattan distance heuristic


def manhattan_distance(state):
distance = 0
for r in range(3):
for c in range(3):
val = state[r][c]
if val != 0:
target_r, target_c = divmod(val - 1, 3)
distance += abs(r - target_r) + abs(c - target_c)
return distance

# Check if the current state is the goal state


def is_goal(state):
return state == goal_state

# Generate possible moves from the current state


def generate_moves(state):
moves = []
r, c = find_blank(state)
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # up, down, left, right
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < 3 and 0 <= nc < 3:
new_state = [row[:] for row in state]
new_state[r][c], new_state[nr][nc] = new_state[nr][nc], new_state[r][c]
moves.append(new_state)
return moves

# A* search algorithm to solve the 8-puzzle problem


def a_star_search(start_state):
open_list = []
closed_list = set()
heapq.heappush(open_list, (manhattan_distance(start_state), 0, start_state, [])) #
(f, g, state, path)

while open_list:
_, g, current_state, path = heapq.heappop(open_list)

if is_goal(current_state):
return path

closed_list.add(tuple(map(tuple, current_state))) # Add state to closed list

for move in generate_moves(current_state):


if tuple(map(tuple, move)) not in closed_list:
heapq.heappush(open_list, (g + 1 + manhattan_distance(move), g + 1,
move, path + [move]))

return None # No solution found

# Function to print the state in a readable format


def print_state(state):
for row in state:
print(row)
print()

# Example usage:
start_state = [[1, 2, 3], [4, 0, 5], [7, 8, 6]] # Start state example
# Define the goal state
goal_state = [[1, 2, 3], [4, 5, 6], [7, 8, 0]]

print("Start State:")
print_state(start_state)

solution = a_star_search(start_state)

if solution:
print("Solution found!")
for step in solution:
print_state(step)
else:
print("No solution found!")

Output:
Start State:
[1, 2, 3]
[4, 0, 5]
[7, 8, 6]

Solution found!
[1, 2, 3]
[4, 5, 0]
[7, 8, 6]

[1, 2, 3]
[4, 5, 6]
[7, 8, 0]

You might also like