Share 8-puzzle problem using best first search in c++

binhminhtian

New member
## Vấn đề 8-puzle sử dụng tìm kiếm đầu tiên tốt nhất trong C ++

Vấn đề 8 trò chơi là một câu đố cổ điển đã được nghiên cứu rộng rãi trong khoa học máy tính.Mục tiêu của câu đố là sắp xếp lại các ô trên bảng 3x3 để chúng theo thứ tự số, từ 1 đến 8. Câu đố được hiển thị bên dưới:

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

Câu đố có thể được giải quyết bằng nhiều thuật toán khác nhau, bao gồm ** Tìm kiếm đầu tiên tốt nhất **.Tìm kiếm đầu tiên tốt nhất là một thuật toán tham lam khám phá không gian trạng thái của câu đố, luôn chọn trạng thái tiếp theo gần với trạng thái mục tiêu nhất.

Để thực hiện tìm kiếm đầu tiên tốt nhất trong C ++, chúng ta có thể sử dụng các bước sau:

1. Xác định cấu trúc dữ liệu để biểu thị các trạng thái của câu đố.
2. Xác định một hàm để tính giá trị ** heuristic ** của trạng thái.Giá trị heuristic là ước tính chi phí đạt được trạng thái mục tiêu từ trạng thái hiện tại.
3. Xác định một chức năng để tạo ra những người kế thừa của một trạng thái.Những người kế vị của một tiểu bang là các quốc gia có thể đạt được bằng cách thực hiện một động thái duy nhất.
4. Xác định một chức năng để tìm trạng thái tiếp theo tốt nhất.Trạng thái tiếp theo tốt nhất là nhà nước có giá trị heuristic thấp nhất.
5. Thực hiện một vòng lặp liên tục gọi hàm `find_best_next_state ()` cho đến khi đạt được trạng thái mục tiêu.

Mã sau đây thực hiện tìm kiếm đầu tiên tốt nhất cho bài toán 8 trò chơi trong C ++:

`` `C ++
#include <Istream>
#include <Vector>

sử dụng không gian tên STD;

// Xác định một cấu trúc dữ liệu để biểu thị các trạng thái của câu đố.
struct state {
Gạch int [3] [3];
};

// Xác định một hàm để tính giá trị heuristic của một trạng thái.
int heuristic (trạng thái nhà nước) {
int chi phí = 0;
for (int i = 0; i <3; i ++) {
for (int j = 0; j <3; j ++) {
if (state.tiles [j]! = 0 && state.tiles [j]! = i * 3 + j + 1) {
chi phí + = abs (state.tiles [j] - i * 3 + j + 1);
}
}
}
chi phí trả lại;
}

// Xác định một chức năng để tạo ra những người kế thừa của một trạng thái.
Vector <Tate> Người kế thừa (trạng thái trạng thái) {
Vector <Tate> kế thừa;
for (int i = 0; i <3; i ++) {
for (int j = 0; j <3; j ++) {
if (state.tiles [j] == 0) {
// di chuyển gạch trống lên.
Người kế thừa nhà nước = Nhà nước;
người kế thừa.tiles [j] = state.tiles [i - 1] [j];
người kế thừa.tiles [i - 1] [j] = 0;
người kế thừa.push_back (người kế thừa);

// Di chuyển gạch trống xuống.
kế thừa = trạng thái;
người kế thừa.tiles [j] = state.tiles [i + 1] [j];
người kế thừa.tiles [i + 1] [j] = 0;
người kế thừa.push_back (người kế thừa);

// di chuyển gạch trống bên trái.
kế thừa = trạng thái;
người kế thừa.tiles [j] = state.tiles [j - 1];
người kế thừa.tiles [j - 1] = 0;
người kế thừa.push_back (người kế thừa);

// Di chuyển gạch trống phải.
kế thừa = trạng thái;
người kế thừa.tiles [j] = state.tiles [j + 1];
người kế thừa.tiles [j + 1] = 0;
người kế thừa.push_back (người kế thừa);
}
}
}
=======================================
## 8-Puzzle Problem Using Best First Search in C++

The 8-puzzle problem is a classic puzzle that has been studied extensively in computer science. The goal of the puzzle is to rearrange the tiles on a 3x3 board so that they are in numerical order, from 1 to 8. The puzzle is shown below:

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

The puzzle can be solved using a variety of different algorithms, including **best first search**. Best first search is a greedy algorithm that iteratively explores the state space of the puzzle, always choosing the next state that is closest to the goal state.

To implement best first search in C++, we can use the following steps:

1. Define a data structure to represent the states of the puzzle.
2. Define a function to calculate the **heuristic** value of a state. The heuristic value is an estimate of the cost of reaching the goal state from the current state.
3. Define a function to generate the successors of a state. The successors of a state are the states that can be reached by making a single move.
4. Define a function to find the best next state. The best next state is the state with the lowest heuristic value.
5. Implement a loop that repeatedly calls the `find_best_next_state()` function until the goal state is reached.

The following code implements best first search for the 8-puzzle problem in C++:

```c++
#include <iostream>
#include <vector>

using namespace std;

// Define a data structure to represent the states of the puzzle.
struct State {
int tiles[3][3];
};

// Define a function to calculate the heuristic value of a state.
int heuristic(State state) {
int cost = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (state.tiles[j] != 0 && state.tiles[j] != i * 3 + j + 1) {
cost += abs(state.tiles[j] - i * 3 + j + 1);
}
}
}
return cost;
}

// Define a function to generate the successors of a state.
vector<State> successors(State state) {
vector<State> successors;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (state.tiles[j] == 0) {
// Move the blank tile up.
State successor = state;
successor.tiles[j] = state.tiles[i - 1][j];
successor.tiles[i - 1][j] = 0;
successors.push_back(successor);

// Move the blank tile down.
successor = state;
successor.tiles[j] = state.tiles[i + 1][j];
successor.tiles[i + 1][j] = 0;
successors.push_back(successor);

// Move the blank tile left.
successor = state;
successor.tiles[j] = state.tiles[j - 1];
successor.tiles[j - 1] = 0;
successors.push_back(successor);

// Move the blank tile right.
successor = state;
successor.tiles[j] = state.tiles[j + 1];
successor.tiles[j + 1] = 0;
successors.push_back(successor);
}
}
}
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top