Share 8-puzzle problem using dfs in c++

haidanghuynh

New member
## Vấn đề 8 trò chơi sử dụng DFS trong C ++

Vấn đề 8 trò chơi 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ự.

Vấn đề 8 hình chữ cái có thể được giải quyết bằng cách sử dụng nhiều thuật toán khác nhau.Một thuật toán phổ biến là tìm kiếm độ sâu đầu tiên (DFS).DFS là một thuật toán đệ quy bắt đầu ở trạng thái ban đầu của câu đố và khám phá tất cả các đường dẫn có thể từ trạng thái đó.Nếu một giải pháp được tìm thấy, DFS trả lại nó.Nếu không tìm thấy giải pháp, DFS trả về null.

Mã sau đây cho thấy cách giải quyết vấn đề 8 trò chơi bằng cách sử dụng DFS trong C ++.

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

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

// bảng 3x3 được biểu thị dưới dạng vectơ 2D
typedef vector <vector <int >> bảng;

// Trạng thái mục tiêu của câu đố
const board Goal_state = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}
};

// một chức năng để kiểm tra xem một bảng ở trạng thái mục tiêu
bool isGoalState (const board & board) {
Trở lại bảng == Goal_State;
}

// một chức năng để tạo tất cả các động tác có thể từ một bảng nhất định
Vector <board> Generatemove (Board & Board) {
Vector <board> di chuyển;

// Đối với mỗi ô trên bảng, tạo một bảng mới bằng cách hoán đổi gạch với
// không gian trống.
for (int i = 0; i <3; i ++) {
for (int j = 0; j <3; j ++) {
if (board [j] == 0) {
// không gian trống ở (i, j).Trao đổi nó với gạch tại (i - 1, j).
Bảng newboard = bảng;
Newboard [j] = newboard [i - 1] [j];
Newboard [i - 1] [j] = 0;
di chuyển.push_back (newboard);

// hoán đổi nó với gạch tại (i + 1, j).
newboard = bảng;
Newboard [j] = newboard [i + 1] [j];
Newboard [i + 1] [j] = 0;
di chuyển.push_back (newboard);

// Trao đổi nó với gạch tại (i, j - 1).
newboard = bảng;
Newboard [j] = newboard [j - 1];
Newboard [j - 1] = 0;
di chuyển.push_back (newboard);

// hoán đổi nó với gạch tại (i, j + 1).
newboard = bảng;
Newboard [j] = newboard [j + 1];
Newboard [j + 1] = 0;
di chuyển.push_back (newboard);
}
}
}

trở lại di chuyển;
}

// một chức năng đệ quy để giải quyết vấn đề 8 trò chơi bằng cách sử dụng DFS
Bảng được giải quyết (bảng điều khiển & bảng điều khiển) {
// Nếu bảng ở trạng thái mục tiêu, hãy trả lại.
if (isGoalState (board)) {
Bảng trở lại;
}

// Tạo tất cả các động tác có thể từ bảng hiện tại.
vector <board> di chuyển = Generatemove (bảng);

// Đối với mỗi di chuyển có thể, giải quyết đệ quy câu đố từ bảng mới.
for (const board & newboard: di chuyển) {
Giải pháp bảng = solvedfs (newboard);
if (giải pháp! = nullptr) {
// Một giải pháp đã được tìm thấy.Trả lại.
giải pháp trả lại;
=======================================
## 8-Puzzle Problem using DFS in C++

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 8-puzzle problem can be solved using a variety of different algorithms. One common algorithm is depth-first search (DFS). DFS is a recursive algorithm that starts at the initial state of the puzzle and explores all possible paths from that state. If a solution is found, DFS returns it. If no solution is found, DFS returns null.

The following code shows how to solve the 8-puzzle problem using DFS in C++.

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

using namespace std;

// A 3x3 board represented as a 2D vector
typedef vector<vector<int>> Board;

// The goal state of the puzzle
const Board GOAL_STATE = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}
};

// A function to check if a board is in the goal state
bool isGoalState(const Board& board) {
return board == GOAL_STATE;
}

// A function to generate all possible moves from a given board
vector<Board> generateMoves(const Board& board) {
vector<Board> moves;

// For each tile on the board, generate a new board by swapping the tile with
// the empty space.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[j] == 0) {
// The empty space is at (i, j). Swap it with the tile at (i - 1, j).
Board newBoard = board;
newBoard[j] = newBoard[i - 1][j];
newBoard[i - 1][j] = 0;
moves.push_back(newBoard);

// Swap it with the tile at (i + 1, j).
newBoard = board;
newBoard[j] = newBoard[i + 1][j];
newBoard[i + 1][j] = 0;
moves.push_back(newBoard);

// Swap it with the tile at (i, j - 1).
newBoard = board;
newBoard[j] = newBoard[j - 1];
newBoard[j - 1] = 0;
moves.push_back(newBoard);

// Swap it with the tile at (i, j + 1).
newBoard = board;
newBoard[j] = newBoard[j + 1];
newBoard[j + 1] = 0;
moves.push_back(newBoard);
}
}
}

return moves;
}

// A recursive function to solve the 8-puzzle problem using DFS
Board solveDFS(const Board& board) {
// If the board is in the goal state, return it.
if (isGoalState(board)) {
return board;
}

// Generate all possible moves from the current board.
vector<Board> moves = generateMoves(board);

// For each possible move, recursively solve the puzzle from the new board.
for (const Board& newBoard : moves) {
Board solution = solveDFS(newBoard);
if (solution != nullptr) {
// A solution was found. Return it.
return solution;
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top