8 puzzle problem using an* algorithm python

blackladybug414

New member
#8puzzs #puzzer #astar #Python

## 8 vấn đề câu đố

8 vấn đề câu đố là một câu đố cổ điển đã được nghiên cứu trong hơn một thế kỷ.Đó là một câu đố đơn giản, nhưng nó cũng rất khó khăn.Mục tiêu của câu đố là di chuyển gạch trên bảng 3x3 để chúng theo đúng thứ tự.

Câu đố được biểu thị bằng lưới 3x3, với mỗi hình vuông chứa một số từ 1 đến 8. Mục tiêu là di chuyển các gạch để chúng theo cùng thứ tự với các số ở phía bên phải của lưới.

Ví dụ: sau đây là 8 câu đố được giải quyết:

`` `
1 2 3
4 5 6
7 8 0
`` `

Để giải câu đố, bạn có thể sử dụng nhiều thuật toán khác nhau.Một thuật toán phổ biến là thuật toán A*.Thuật toán A* là một thuật toán tham lam cố gắng tìm con đường ngắn nhất đến mục tiêu.

Thuật toán A* hoạt động bằng cách tạo ra một biểu đồ về các trạng thái có thể của câu đố.Mỗi trạng thái được biểu diễn bởi một nút trong biểu đồ.Các cạnh trong biểu đồ thể hiện các động tác có thể có thể được thực hiện từ trạng thái này sang trạng thái khác.

Thuật toán A* sau đó bắt đầu ở trạng thái ban đầu và cố gắng tìm con đường ngắn nhất đến trạng thái mục tiêu.Nó thực hiện điều này bằng cách đánh giá chi phí của từng động thái có thể và chọn động thái với chi phí thấp nhất.

Thuật toán A* tiếp tục khám phá biểu đồ cho đến khi nó tìm thấy trạng thái mục tiêu.Độ phức tạp về thời gian của thuật toán A* là O (B^d), trong đó B là yếu tố phân nhánh của cây tìm kiếm và D là độ sâu của cây.

Sau đây là việc triển khai thuật toán A* trong Python:

`` `Python
nhập khẩu đống

def astar (bắt đầu, mục tiêu):
"" "
Giải quyết vấn đề 8 câu đố bằng thuật toán A*.

Thông số:
Bắt đầu: Trạng thái bắt đầu của câu đố.
Mục tiêu: Trạng thái mục tiêu của câu đố.

Trả lại:
Trình tự di chuyển giải quyết câu đố.
"" "

# Tạo một biểu đồ về các trạng thái có thể của câu đố.

đồ thị = {}
Đối với tôi trong phạm vi (3):
Đối với J trong phạm vi (3):
trạng thái = (i, j)
đồ thị [trạng thái] = []

# Thêm các động tác có thể từ trạng thái này vào biểu đồ.

Đối với k trong phạm vi (4):
new_i = i + dirs [k] [0]
new_j = j + dirs [k] [1]
Nếu 0 <= new_i <3 và 0 <= new_j <3:
new_state = (new_i, new_j)
đồ thị [state] .Append (new_state)

# Tạo một hàng đợi ưu tiên để lưu trữ các trạng thái được khám phá.

Hàng đợi = []
Heapq.HeAppush (Hàng đợi, (0, bắt đầu))

# Tạo một bộ để lưu trữ các trạng thái được khám phá.

đã khám phá = set ()

# Trong khi vẫn còn các tiểu bang được khám phá ...

Trong khi xếp hàng:
# Nhận trạng thái tiếp theo được khám phá.

_, state = heapq.HeAppop (hàng đợi)

# Nếu trạng thái là trạng thái mục tiêu, hãy trả lại giải pháp.

Nếu trạng thái == Mục tiêu:
trả lại get_solution (trạng thái)

# Nếu nhà nước chưa được khám phá, hãy khám phá nó.

Nếu trạng thái không được khám phá:
đã khám phá.Add (trạng thái)

# Đối với mỗi lần di chuyển có thể từ trạng thái này ...

cho new_state trong đồ thị [trạng thái]:
# Tính chi phí chuyển sang trạng thái mới này.

chi phí = manhattan_distance (new_state, mục tiêu)

# Tạo một nút mới cho trạng thái mới này.

node = (chi phí, new_state)

#
=======================================
#8puzzle #puzzle #astar #Python

## 8 Puzzle Problem

The 8 puzzle problem is a classic puzzle that has been studied for over a century. It is a simple puzzle, but it is also very challenging. The goal of the puzzle is to move the tiles on a 3x3 board so that they are in the correct order.

The puzzle is represented by a 3x3 grid, with each square containing a number from 1 to 8. The goal is to move the tiles so that they are in the same order as the numbers on the right side of the grid.

For example, the following is a solved 8 puzzle:

```
1 2 3
4 5 6
7 8 0
```

To solve the puzzle, you can use a variety of different algorithms. One popular algorithm is the A* algorithm. The A* algorithm is a greedy algorithm that tries to find the shortest path to the goal.

The A* algorithm works by first creating a graph of the possible states of the puzzle. Each state is represented by a node in the graph. The edges in the graph represent the possible moves that can be made from one state to another.

The A* algorithm then starts at the initial state and tries to find the shortest path to the goal state. It does this by evaluating the cost of each possible move and choosing the move with the lowest cost.

The A* algorithm continues to explore the graph until it finds the goal state. The time complexity of the A* algorithm is O(b^d), where b is the branching factor of the search tree and d is the depth of the tree.

The following is an implementation of the A* algorithm in Python:

```python
import heapq

def astar(start, goal):
"""
Solves the 8 puzzle problem using the A* algorithm.

Parameters:
start: The starting state of the puzzle.
goal: The goal state of the puzzle.

Returns:
The sequence of moves that solves the puzzle.
"""

# Create a graph of the possible states of the puzzle.

graph = {}
for i in range(3):
for j in range(3):
state = (i, j)
graph[state] = []

# Add the possible moves from this state to the graph.

for k in range(4):
new_i = i + dirs[k][0]
new_j = j + dirs[k][1]
if 0 <= new_i < 3 and 0 <= new_j < 3:
new_state = (new_i, new_j)
graph[state].append(new_state)

# Create a priority queue to store the states to be explored.

queue = []
heapq.heappush(queue, (0, start))

# Create a set to store the explored states.

explored = set()

# While there are still states to be explored...

while queue:
# Get the next state to be explored.

_, state = heapq.heappop(queue)

# If the state is the goal state, return the solution.

if state == goal:
return get_solution(state)

# If the state has not been explored yet, explore it.

if state not in explored:
explored.add(state)

# For each possible move from this state...

for new_state in graph[state]:
# Calculate the cost of moving to this new state.

cost = manhattan_distance(new_state, goal)

# Create a new node for this new state.

node = (cost, new_state)

#
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock