八数码问题c++可视化

时间: 2023-08-29 16:12:14 浏览: 38
八数码问题是一个经典的搜索问题,可以使用C++和可视化工具来实现。以下是一个简单的实现: ```c++ #include <iostream> #include <iomanip> #include <queue> #include <unordered_set> using namespace std; // 定义状态结构体 struct State { int board[3][3]; int zero_row, zero_col; int move; // 移动的方向,0表示不动,1表示上,2表示下,3表示左,4表示右 State() {}; State(const State& other) { memcpy(this->board, other.board, sizeof(board)); this->zero_row = other.zero_row; this->zero_col = other.zero_col; this->move = other.move; } bool operator==(const State& other) const { return memcmp(board, other.board, sizeof(board)) == 0; } }; // 定义哈希函数 struct Hash { size_t operator()(const State& state) const { size_t h = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { h = h * 10 + state.board[i][j]; } } return h; } }; // 定义查找方向数组 const int dx[] = {0, -1, 1, 0, 0}; const int dy[] = {0, 0, 0, -1, 1}; // 定义目标状态 const int target_board[3][3] = { {1, 2, 3}, {8, 0, 4}, {7, 6, 5} }; // 判断状态是否合法 bool is_valid(const State& state) { int inv = 0; int a[9]; int k = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { a[k++] = state.board[i][j]; } } for (int i = 0; i < 9; i++) { if (a[i] == 0) { continue; } for (int j = 0; j < i; j++) { if (a[j] == 0) { continue; } if (a[j] > a[i]) { inv++; } } } return inv % 2 == 0; } // 计算启发函数 int heuristic(const State& state) { int h = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (state.board[i][j] == 0) { continue; } int x = (state.board[i][j] - 1) / 3; int y = (state.board[i][j] - 1) % 3; h += abs(i - x) + abs(j - y); } } return h; } // 打印状态 void print_state(const State& state) { cout << "Move: " << state.move << endl; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout << setw(2) << state.board[i][j] << " "; } cout << endl; } cout << endl; } // 搜索函数 int solve(State& start_state) { unordered_set<State, Hash> visited; priority_queue<pair<int, State>> pq; start_state.move = 0; visited.insert(start_state); pq.push({-heuristic(start_state), start_state}); while (!pq.empty()) { State state = pq.top().second; pq.pop(); if (memcmp(state.board, target_board, sizeof(target_board)) == 0) { // 打印解 vector<State> path; while (state.move != 0) { path.push_back(state); state = path.back(); } path.push_back(state); for (int i = path.size() - 1; i >= 0; i--) { print_state(path[i]); } return path.size() - 1; } int zero_x = state.zero_row; int zero_y = state.zero_col; for (int i = 1; i <= 4; i++) { int new_zero_x = zero_x + dx[i]; int new_zero_y = zero_y + dy[i]; if (new_zero_x < 0 || new_zero_x >= 3 || new_zero_y < 0 || new_zero_y >= 3) { continue; } State new_state = state; swap(new_state.board[zero_x][zero_y], new_state.board[new_zero_x][new_zero_y]); new_state.zero_row = new_zero_x; new_state.zero_col = new_zero_y; new_state.move = i; if (visited.count(new_state) == 0) { visited.insert(new_state); pq.push({-heuristic(new_state), new_state}); } } } return -1; } int main() { State start_state; cout << "请输入初始状态(用空格分隔数字,0表示空格):" << endl; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cin >> start_state.board[i][j]; if (start_state.board[i][j] == 0) { start_state.zero_row = i; start_state.zero_col = j; } } } if (!is_valid(start_state)) { cout << "无解" << endl; return 0; } int steps = solve(start_state); if (steps == -1) { cout << "无解" << endl; } else { cout << "步数:" << steps << endl; } return 0; } ``` 这个实现中使用了哈希表和启发式搜索来加速搜索过程,并且支持任意初始状态的输入。可以使用Qt等可视化工具来实现界面交互部分。

相关推荐

最新推荐

recommend-type

人工智能 A*算法 八数码问题 C++ 报告+代码+详细注释

使用C++语言完整的实现了A星算法解决八数码问题 内容:完整代码和详细注释; 主要函数的功能说明; 评价函数的设计; 运行测试结果
recommend-type

约瑟夫环问题用C++代码实现

8. 【题目】约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为1的人开始报数,数到k的那个人出列;他的下一个人又从1开始报数,数到k的那个人又...
recommend-type

vscode使用官方C/C++插件无法进行代码格式化问题

官方的C/C++插件是支持使用.clang-format配置文件进行自定义风格代码格式化的,无需另外安装clang-format插件。 但是使用clang-format -style=llvm -dump-config &gt; .clang-format导出的默认配置文件进行格式化的时候...
recommend-type

C++类的静态成员初始化详细讲解

通常静态数据成员在类声明中声明,在包含类方法的文件中初始化.初始化时使用作用域操作符来指出静态成员所属的类.但如果静态成员是整型或是枚举型const,则可以在类声明中初始化
recommend-type

可视化实验二实验报告.doc

一个可视化实验完整代码。 一、实验目的和要求 1、 使用API函数编制包含各种资源的Windows应用程序; 2、 实现Windows应用程序对键盘与鼠标的响应。 二、实验内容 : 1、问题描述: 创建一个菜单,其中有...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SPDK_NVMF_DISCOVERY_NQN是什么 有什么作用

SPDK_NVMF_DISCOVERY_NQN 是 SPDK (Storage Performance Development Kit) 中用于查询 NVMf (Non-Volatile Memory express over Fabrics) 存储设备名称的协议。NVMf 是一种基于网络的存储协议,可用于连接远程非易失性内存存储器。 SPDK_NVMF_DISCOVERY_NQN 的作用是让存储应用程序能够通过 SPDK 查询 NVMf 存储设备的名称,以便能够访问这些存储设备。通过查询 NVMf 存储设备名称,存储应用程序可以获取必要的信息,例如存储设备的IP地址、端口号、名称等,以便能
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。