Share 8 puzzle c++,

redfrog272

New member
#8puzz, #C ++, #puzz, #Programming, #AlGorithM ### 8 Câu đố trong 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 viên gạch xung quanh trên lưới 3x3 để chúng được sắp xếp 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.

8 câu đố có thể được giải quyết bằng nhiều thuật toán khác nhau, nhưng một trong những thuật toán hiệu quả nhất là thuật toán A*.Thuật toán A* hoạt động bằng cách lặp đi lặp lại không gian tìm kiếm, bắt đầu từ trạng thái ban đầu và chuyển sang các trạng thái gần với trạng thái mục tiêu hơn.Thuật toán theo dõi chi phí của mỗi tiểu bang và nó chỉ khám phá các trạng thái có chi phí thấp hơn trạng thái hiện tại.Điều này đảm bảo rằng thuật toán cuối cùng sẽ tìm thấy trạng thái mục tiêu và nó sẽ làm như vậy trong ít thời gian nhất.

Mã sau đây thực hiện thuật toán* để giải 8 câu đố trong C ++.

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

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

// đại diện cho một trạng thái của 8 câu đố.
struct state {
Gạch int [3] [3];
};

// Trả về chi phí của một trạng thái nhất định.
int get_cost (trạng thái trạng thái) {
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í ++;
}
}
}
chi phí trả lại;
}

// trả lại hàng xóm của một quốc gia nhất định.
Vector <tate> get_neighbors (trạng thái trạng thái) {
Vector <Tate> Hàng xóm;

// Nhận vị trí của gạch trống.
int blank_i = -1;
int blank_j = -1;
for (int i = 0; i <3; i ++) {
for (int j = 0; j <3; j ++) {
if (state.tiles [j] == 0) {
blank_i = i;
Blank_j = j;
}
}
}

// Thêm hàng xóm của gạch trống vào danh sách.
if (blank_i> 0) {
hàng xóm.push_back (trạng thái);
state.tiles [blank_i] [blank_j] = state.tiles [blank_i - 1] [blank_j];
state.tiles [blank_i - 1] [blank_j] = 0;
}
if (blank_i <2) {
hàng xóm.push_back (trạng thái);
state.tiles [blank_i] [blank_j] = state.tiles [blank_i + 1] [blank_j];
state.tiles [blank_i + 1] [blank_j] = 0;
}
if (blank_j> 0) {
hàng xóm.push_back (trạng thái);
state.tiles [blank_i] [blank_j] = state.tiles [blank_i] [blank_j - 1];
state.tiles [blank_i] [blank_j - 1] = 0;
}
if (blank_j <2) {
hàng xóm.push_back (trạng thái);
state.tiles [blank_i] [blank_j] = state.tiles [blank_i] [blank_j + 1];
state.tiles [blank_i] [blank_j + 1] = 0;
}

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

// Trả về đúng nếu một trạng thái nhất định là trạng thái mục tiêu.
bool is_goal (trạng thái trạng thái) {
=======================================
#8puzzle, #C++, #puzzle, #Programming, #AlGorithM ### 8 Puzzle in C++

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 on a 3x3 grid so that they are arranged in the correct order, with the 1 tile in the top left corner, the 2 tile in the top right corner, and so on.

The 8 puzzle can be solved using a variety of different algorithms, but one of the most efficient is the A* algorithm. The A* algorithm works by iteratively exploring the search space, starting from the initial state and moving to states that are closer to the goal state. The algorithm keeps track of the cost of each state, and it only explores states that have a lower cost than the current state. This ensures that the algorithm will eventually find the goal state, and it will do so in the least amount of time.

The following code implements the A* algorithm for solving the 8 puzzle in C++.

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

using namespace std;

// Represents a state of the 8 puzzle.
struct State {
int tiles[3][3];
};

// Returns the cost of a given state.
int get_cost(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++;
}
}
}
return cost;
}

// Returns the neighbors of a given state.
vector<State> get_neighbors(State state) {
vector<State> neighbors;

// Get the position of the blank tile.
int blank_i = -1;
int blank_j = -1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (state.tiles[j] == 0) {
blank_i = i;
blank_j = j;
}
}
}

// Add the neighbors of the blank tile to the list.
if (blank_i > 0) {
neighbors.push_back(state);
state.tiles[blank_i][blank_j] = state.tiles[blank_i - 1][blank_j];
state.tiles[blank_i - 1][blank_j] = 0;
}
if (blank_i < 2) {
neighbors.push_back(state);
state.tiles[blank_i][blank_j] = state.tiles[blank_i + 1][blank_j];
state.tiles[blank_i + 1][blank_j] = 0;
}
if (blank_j > 0) {
neighbors.push_back(state);
state.tiles[blank_i][blank_j] = state.tiles[blank_i][blank_j - 1];
state.tiles[blank_i][blank_j - 1] = 0;
}
if (blank_j < 2) {
neighbors.push_back(state);
state.tiles[blank_i][blank_j] = state.tiles[blank_i][blank_j + 1];
state.tiles[blank_i][blank_j + 1] = 0;
}

return neighbors;
}

// Returns true if a given state is the goal state.
bool is_goal(State state) {
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top