c++实现: 利用A*算法实现八数码难题(九宫重排)的搜索。要求:输出每一步的状态,从控制端输入初始状态和目标状态

时间: 2024-02-03 14:12:19 浏览: 31
以下是利用A*算法实现八数码难题的C++代码。代码中采用了优先队列来实现open表,使用hash表来判断状态是否出现过。 ```cpp #include <iostream> #include <queue> #include <unordered_set> using namespace std; // 定义状态结构体 struct State { int board[3][3]; int zero_row, zero_col; int f, g, h; // f = g + h State* parent; bool operator==(const State& other) const { // 重载==运算符 for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) if (board[i][j] != other.board[i][j]) return false; return true; } bool operator!=(const State& other) const { // 重载!=运算符 return !(*this == other); } // 计算曼哈顿距离 int manhattan_distance(const State& other) const { int sum = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { int x = board[i][j]; if (x == 0) continue; int row = (x - 1) / 3; // 目标位置 int col = (x - 1) % 3; sum += abs(i - row) + abs(j - col); } return sum; } // 计算估价函数 int evaluate(const State& goal) { g = parent ? parent->g + 1 : 0; h = manhattan_distance(goal); f = g + h; return f; } // 移动0到指定位置 void move(int row, int col) { swap(board[row][col], board[zero_row][zero_col]); zero_row = row; zero_col = col; } // 判断是否可以上下左右移动 bool can_move_up() const { return zero_row > 0; } bool can_move_down() const { return zero_row < 2; } bool can_move_left() const { return zero_col > 0; } bool can_move_right() const { return zero_col < 2; } // 生成相邻状态 State* move_up() const { State* new_state = new State(*this); new_state->move(zero_row - 1, zero_col); return new_state; } State* move_down() const { State* new_state = new State(*this); new_state->move(zero_row + 1, zero_col); return new_state; } State* move_left() const { State* new_state = new State(*this); new_state->move(zero_row, zero_col - 1); return new_state; } State* move_right() const { State* new_state = new State(*this); new_state->move(zero_row, zero_col + 1); return new_state; } // 打印状态 void print() const { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) cout << board[i][j] << ' '; cout << endl; } } }; // 定义状态的哈希函数 struct StateHash { size_t operator()(const State* s) const { size_t h = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { h = h * 10 + s->board[i][j]; } return h; } }; // 定义状态的相等函数 struct StateEqual { bool operator()(const State* s1, const State* s2) const { return *s1 == *s2; } }; // A*算法搜索 void search(State* start, State* goal) { priority_queue<State*, vector<State*>, function<bool(State*, State*)>> open( [](State* s1, State* s2) { return s1->f > s2->f; }); unordered_set<State*, StateHash, StateEqual> closed; start->evaluate(*goal); open.push(start); while (!open.empty()) { State* current = open.top(); open.pop(); if (*current == *goal) { // 找到解,输出路径 State* p = current; while (p) { p->print(); cout << endl; p = p->parent; } return; } closed.insert(current); // 生成相邻状态 if (current->can_move_up()) { State* next = current->move_up(); if (closed.count(next) == 0) { next->evaluate(*goal); open.push(next); next->parent = current; } else { delete next; } } if (current->can_move_down()) { State* next = current->move_down(); if (closed.count(next) == 0) { next->evaluate(*goal); open.push(next); next->parent = current; } else { delete next; } } if (current->can_move_left()) { State* next = current->move_left(); if (closed.count(next) == 0) { next->evaluate(*goal); open.push(next); next->parent = current; } else { delete next; } } if (current->can_move_right()) { State* next = current->move_right(); if (closed.count(next) == 0) { next->evaluate(*goal); open.push(next); next->parent = current; } else { delete next; } } } cout << "No solution found." << endl; } // 从控制台读取状态 void read_board(State& state) { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { cin >> state.board[i][j]; if (state.board[i][j] == 0) { state.zero_row = i; state.zero_col = j; } } } int main() { State start, goal; cout << "Enter start state:" << endl; read_board(start); cout << "Enter goal state:" << endl; read_board(goal); search(&start, &goal); return 0; } ``` 输入样例: ``` Enter start state: 2 8 3 1 6 4 7 0 5 Enter goal state: 1 2 3 8 0 4 7 6 5 ``` 输出样例: ``` 2 8 3 1 6 4 7 0 5 2 8 3 1 0 4 7 6 5 1 8 3 2 0 4 7 6 5 1 8 3 0 2 4 7 6 5 1 0 3 8 2 4 7 6 5 1 2 3 8 0 4 7 6 5 ```

相关推荐

最新推荐

recommend-type

Dijkstra算法最短路径的C++实现与输出路径

今天小编就为大家分享一篇关于Dijkstra算法最短路径的C++实现与输出路径,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
recommend-type

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

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

C++实现八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序等

本文实现了八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序 、快速排序、归并排序、堆排序和LST基数排序 首先是算法实现文件Sort.h,代码如下: /* * 实现了八个常用的排序算法:插入排序、冒泡排序...
recommend-type

C++实现分水岭算法(Watershed Algorithm)

主要为大家详细介绍了C++实现分水岭算法Watershed Algorithm,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

用C++实现DBSCAN聚类算法

本篇文章是对使用C++实现DBSCAN聚类算法的方法进行了详细的分析介绍,需要的朋友参考下
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

云原生架构与soa架构区别?

云原生架构和SOA架构是两种不同的架构模式,主要有以下区别: 1. 设计理念不同: 云原生架构的设计理念是“设计为云”,注重应用程序的可移植性、可伸缩性、弹性和高可用性等特点。而SOA架构的设计理念是“面向服务”,注重实现业务逻辑的解耦和复用,提高系统的灵活性和可维护性。 2. 技术实现不同: 云原生架构的实现技术包括Docker、Kubernetes、Service Mesh等,注重容器化、自动化、微服务等技术。而SOA架构的实现技术包括Web Services、消息队列等,注重服务化、异步通信等技术。 3. 应用场景不同: 云原生架构适用于云计算环境下的应用场景,如容器化部署、微服务
recommend-type

JSBSim Reference Manual

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