Share 8 puzzle c++ source code

baohuynhbadman

New member
## 8 Mã nguồn C ++

8 câu đố là một trò chơi giải đố cổ điển đã xuất hiện trong nhiều thế kỷ.Mục tiêu của trò chơi là di chuyển các gạch xung quanh để chúng theo đúng thứ tự, với 1 gạch ở góc trên cùng bên trái, 2 gạch ở góc trên cùng bên phải, v.v.

Bài viết này cung cấp mã nguồn C ++ cho 8 câu đố.Mã được thông phân tốt và dễ hiểu.Nó cũng bao gồm một chức năng chính đơn giản mà bạn có thể sử dụng để kiểm tra mã.

### Mã số

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

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

// Xác định 8 bảng câu đố
const int board_size = 3;
bo mạch int [board_size] [board_size] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}
};

// Xác định các động tác có thể cho gạch trống
int di chuyển [4] [2] = {
{-1, 0}, // Up
{1, 0}, // Down
{0, -1}, //
{0, 1} // đúng
};

// kiểm tra xem câu đố có được giải quyết không
bool phát hành () {
for (int i = 0; i <board_size; i ++) {
for (int j = 0; j <board_size; j ++) {
if (board [j]! = i * board_size + j + 1) {
trả lại sai;
}
}
}

trả lại đúng;
}

// Giải câu đố bằng thuật toán*
void giải quyết () {
// Tạo hàng đợi ưu tiên để lưu trữ các trạng thái mở
vector <cặp <int, int >> mở;

// Thêm trạng thái ban đầu vào hàng đợi ưu tiên
open.push_back (make_pair (0, board));

// Trong khi vẫn còn trạng thái mở
while (! open.empty ()) {
// Nhận trạng thái với mức độ ưu tiên thấp nhất
cặp <int, int> state = open.front ();
open.erase (open.begin ());

// Nếu trạng thái được giải quyết, chúng tôi đã hoàn thành
if (phát hành ()) {
phá vỡ;
}

// Nhận hàng xóm của nhà nước
vector <cặp <int, int >> hàng xóm = getNeighBors (state.second);

// Đối với mỗi người hàng xóm, hãy tính quyền ưu tiên của nó
for (int i = 0; i <neighbors.size (); i ++) {
int ưu tiên = state.first + 1 + manhattandistance (hàng xóm .second);

// Nếu hàng xóm không có trong danh sách mở, hãy thêm nó
if (find (open.begin (), open.end (), hàng xóm ) == open.end ()) {
open.push_back (make_pair (ưu tiên, hàng xóm .second));
}
}
}
}

// Nhận hàng xóm của một tiểu bang
vector <cặp <int, int >> getNeighBors (int board [board_size] [board_size]) {
vector <cặp <int, int >> hàng xóm;

for (int i = 0; i <4; i ++) {
int x = board [0] [0] + di chuyển [0];
int y = board [0] [0] + di chuyển [1];

if (x> = 0 && x <board_size && y> = 0 && y <board_size) {
hàng xóm.push_back (make_pair (0, bảng));
}
}

trở về hàng xóm;
}

// Tính khoảng cách Manhattan giữa hai trạng thái
int manhattandistance (board int [board_size] [board_size]) {
khoảng cách int = 0;
=======================================
## 8 Puzzle C++ Source Code

The 8 puzzle is a classic puzzle game that has been around for centuries. The goal of the game is to move the tiles around so that they are in the correct order, with the 1 tile in the top left corner, the 2 tile in the top right corner, and so on.

This article provides a C++ source code for the 8 puzzle. The code is well-commented and easy to understand. It also includes a simple main function that you can use to test the code.

### Code

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

using namespace std;

// Define the 8 puzzle board
const int BOARD_SIZE = 3;
int board[BOARD_SIZE][BOARD_SIZE] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}
};

// Define the possible moves for the blank tile
int moves[4][2] = {
{-1, 0}, // up
{1, 0}, // down
{0, -1}, // left
{0, 1} // right
};

// Check if the puzzle is solved
bool isSolved() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[j] != i * BOARD_SIZE + j + 1) {
return false;
}
}
}

return true;
}

// Solve the puzzle using the A* algorithm
void solve() {
// Create a priority queue to store the open states
vector<pair<int, int>> open;

// Add the initial state to the priority queue
open.push_back(make_pair(0, board));

// While there are still open states
while (!open.empty()) {
// Get the state with the lowest priority
pair<int, int> state = open.front();
open.erase(open.begin());

// If the state is solved, we're done
if (isSolved()) {
break;
}

// Get the neighbors of the state
vector<pair<int, int>> neighbors = getNeighbors(state.second);

// For each neighbor, calculate its priority
for (int i = 0; i < neighbors.size(); i++) {
int priority = state.first + 1 + manhattanDistance(neighbors.second);

// If the neighbor is not in the open list, add it
if (find(open.begin(), open.end(), neighbors) == open.end()) {
open.push_back(make_pair(priority, neighbors.second));
}
}
}
}

// Get the neighbors of a state
vector<pair<int, int>> getNeighbors(int board[BOARD_SIZE][BOARD_SIZE]) {
vector<pair<int, int>> neighbors;

for (int i = 0; i < 4; i++) {
int x = board[0][0] + moves[0];
int y = board[0][0] + moves[1];

if (x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE) {
neighbors.push_back(make_pair(0, board));
}
}

return neighbors;
}

// Calculate the Manhattan distance between two states
int manhattanDistance(int board[BOARD_SIZE][BOARD_SIZE]) {
int distance = 0;
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top