c++程序

时间: 2023-07-30 10:06:37 浏览: 41
以下是一个使用C++语言编写的八数码难题解决方案程序示例: ```c++ #include <iostream> #include <vector> #include <queue> #include <unordered_set> using namespace std; // 定义状态结构体 struct State { vector<vector<int>> board; // 数字方块的布局 int cost; // 代价值 int blank_x; // 空白方块的横坐标 int blank_y; // 空白方块的纵坐标 int step; // 步数 string path; // 移动路径 State(vector<vector<int>>& b, int c, int x, int y, int s, string p) : board(b), cost(c), blank_x(x), blank_y(y), step(s), path(p) {} }; // 定义哈希函数和判断相等函数 namespace std { template <> struct hash<State> { 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 = 31 * h + s.board[i][j]; } } return h; } }; template <> struct equal_to<State> { bool operator()(const State& s1, const State& s2) const { return s1.board == s2.board; } }; } // 定义启发式函数 int heuristic(State& s, vector<vector<int>>& target) { int h = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { int num = s.board[i][j]; if (num == 0) continue; int x = (num - 1) / 3; int y = (num - 1) % 3; h += abs(i - x) + abs(j - y); } } return h; } // 定义A*搜索函数 string solve(State& start, vector<vector<int>>& target) { priority_queue<State, vector<State>, function<bool(State&, State&)>> q([](State& s1, State& s2) { return s1.cost > s2.cost; }); unordered_set<State> visited; q.push(start); visited.insert(start); while (!q.empty()) { State s = q.top(); q.pop(); if (s.board == target) { return s.path; } int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; for (int i = 0; i < 4; i++) { int nx = s.blank_x + dx[i]; int ny = s.blank_y + dy[i]; if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3) continue; vector<vector<int>> nb = s.board; swap(nb[nx][ny], nb[s.blank_x][s.blank_y]); State ns(nb, s.step + 1 + heuristic(State(nb, 0, nx, ny, 0, ""), target), nx, ny, s.step + 1, s.path + to_string(nb[nx][ny]) + (i == 0 ? "u" : i == 1 ? "r" : i == 2 ? "d" : "l")); if (visited.find(ns) == visited.end()) { q.push(ns); visited.insert(ns); } } } return "unsolvable"; } int main() { vector<vector<int>> start = {{2, 8, 3}, {1, 6, 4}, {7, 0, 5}}; vector<vector<int>> target = {{1, 2, 3}, {8, 0, 4}, {7, 6, 5}}; State s(start, 0, 1, 2, 0, ""); string path = solve(s, target); cout << path << endl; return 0; } ``` 该程序使用了STL库中的vector、queue和unordered_set容器,以及lambda表达式和函数对象等C++11特性。程序中使用了哈希函数和判断相等函数对状态进行处理,以便于在unordered_set中进行查找和插入。启发式函数使用了曼哈顿距离来估计从当前状态到目标状态的距离。A*搜索函数中使用了一个优先队列来按照代价值进行搜索,并使用了一个unordered_set来记录已经访问过的状态,避免重复搜索。程序最终返回的是一个移动路径字符串,例如"8123ur5l4d"表示先将数字8向上移动一格,然后将数字1、2、3向右移动一格,再将数字5向左移动一格,最后将数字4向下移动一格,即可将初始状态变为目标状态。

相关推荐

最新推荐

recommend-type

在C++程序中开启和禁用Windows设备的无线网卡的方法

主要介绍了在C++程序中开启和禁用Windows设备的无线网卡的方法,包括一些常见错误的分析与解决,需要的朋友可以参考下
recommend-type

C++程序移植及vs远程开发_从windows到linux程序.docx

本人亲测windows下的c++控制台程序移植到linux环境下,且可以通过windows+vs进行远程开发linux程序,总结文档可供学习!
recommend-type

Python调用C++程序的方法详解

主要介绍了Python调用C++程序的方法,文中通过示例代码介绍的详细,相信对大家具有一定的参考借鉴价值,需要的朋友们下面来一起看看吧。
recommend-type

c++程序设计 南京理工大学

c++程序设计 南京理工大学c++程序设计 南京理工大学c++程序设计 南京理工大学
recommend-type

c++程序设计知识点总结(谭浩强).docx

自己整理的山东大学C++考试重点 自己整理的山东大学C++考试重点 自己整理的山东大学C++考试重点
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

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

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