A*算法之八数码问题(java代码)

时间: 2023-09-04 14:08:13 浏览: 49
以下是使用A*算法解决八数码问题的Java代码: ``` import java.util.*; public class EightPuzzleSolver { private static final int[] GOAL = {1, 2, 3, 4, 5, 6, 7, 8, 0}; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter initial state of the puzzle (0 represents blank tile):"); int[] initialState = new int[9]; for (int i = 0; i < 9; i++) { initialState[i] = scanner.nextInt(); } EightPuzzleSolver solver = new EightPuzzleSolver(); List<State> solution = solver.solve(initialState); if (solution == null) { System.out.println("No solution found!"); } else { System.out.println("Solution steps:"); for (State state : solution) { System.out.println(state); } } } private PriorityQueue<State> open; private Set<State> closed; public EightPuzzleSolver() { open = new PriorityQueue<>(); closed = new HashSet<>(); } public List<State> solve(int[] initialState) { State initial = new State(initialState, null, 0, getHeuristic(initialState)); open.add(initial); while (!open.isEmpty()) { State current = open.poll(); if (Arrays.equals(current.tiles, GOAL)) { List<State> solution = new ArrayList<>(); while (current != null) { solution.add(current); current = current.parent; } Collections.reverse(solution); return solution; } closed.add(current); int blankIndex = getBlankIndex(current.tiles); if (blankIndex - 3 >= 0) { int[] newTiles = swap(current.tiles, blankIndex, blankIndex - 3); State newState = new State(newTiles, current, current.moves + 1, getHeuristic(newTiles)); if (!closed.contains(newState)) { open.add(newState); } } if (blankIndex + 3 < 9) { int[] newTiles = swap(current.tiles, blankIndex, blankIndex + 3); State newState = new State(newTiles, current, current.moves + 1, getHeuristic(newTiles)); if (!closed.contains(newState)) { open.add(newState); } } if (blankIndex % 3 != 0) { int[] newTiles = swap(current.tiles, blankIndex, blankIndex - 1); State newState = new State(newTiles, current, current.moves + 1, getHeuristic(newTiles)); if (!closed.contains(newState)) { open.add(newState); } } if (blankIndex % 3 != 2) { int[] newTiles = swap(current.tiles, blankIndex, blankIndex + 1); State newState = new State(newTiles, current, current.moves + 1, getHeuristic(newTiles)); if (!closed.contains(newState)) { open.add(newState); } } } return null; } private int getHeuristic(int[] tiles) { int heuristic = 0; for (int i = 0; i < 9; i++) { if (tiles[i] != 0) { int rowDist = Math.abs(i / 3 - (tiles[i] - 1) / 3); int colDist = Math.abs(i % 3 - (tiles[i] - 1) % 3); heuristic += rowDist + colDist; } } return heuristic; } private int getBlankIndex(int[] tiles) { for (int i = 0; i < 9; i++) { if (tiles[i] == 0) { return i; } } return -1; } private int[] swap(int[] tiles, int i, int j) { int[] newTiles = Arrays.copyOf(tiles, tiles.length); int temp = newTiles[i]; newTiles[i] = newTiles[j]; newTiles[j] = temp; return newTiles; } private static class State implements Comparable<State> { private int[] tiles; private State parent; private int moves; private int heuristic; public State(int[] tiles, State parent, int moves, int heuristic) { this.tiles = tiles; this.parent = parent; this.moves = moves; this.heuristic = heuristic; } @Override public int compareTo(State other) { return Integer.compare(moves + heuristic, other.moves + other.heuristic); } @Override public boolean equals(Object other) { if (other instanceof State) { return Arrays.equals(tiles, ((State) other).tiles); } return false; } @Override public int hashCode() { return Arrays.hashCode(tiles); } @Override public String toString() { StringBuilder builder = new StringBuilder(); for (int i = 0; i < 9; i++) { builder.append(tiles[i]); if (i % 3 == 2) { builder.append("\n"); } else { builder.append(" "); } } return builder.toString(); } } } ``` 这个程序使用A*算法来找到八数码问题的解决方案。它使用一个优先队列来存储open状态,每次从队列中取出f值最小的状态进行扩展,并将其添加到closed集合中。我们还定义了一个State类来表示每个状态,包括当前状态的tiles数组,父状态,移动次数和启发式估计值。在getHeuristic方法中,我们使用曼哈顿距离作为启发式函数。最后,我们使用swap方法来交换空白图块和其他图块之间的位置。

相关推荐

最新推荐

recommend-type

Elasticsearch初识与简单案例.pdf

Elasticsearch是一个基于Lucene的分布式全文搜索引擎,提供灵活且高效的搜索和分析功能。通过HTTP请求和客户端库,用户可以索引和搜索文档,执行复杂查询,进行数据分析,并享受高亮显示等特性。其高级功能如复合查询、聚合分析、滚动搜索等,使其适用于各种数据处理和分析场景。Elasticsearch还具有强大的监控和日志功能,确保集群稳定运行。总之,Elasticsearch是企业级搜索和分析的理想选择。
recommend-type

Python基于LSTM模型对全国的空气质量数据进行可视化分析预测源代码

介绍 对全国2019年1月至2023年12月的空气质量数据进行分析,绘制时间序列图,展示每月/每季度的平均AQI变化趋势。绘制不同省份和城市的平均AQI热力图。分析不同污染物的浓度分布和趋势。绘制空气质量等级分布图。 需求说明 对空气质量数据进行数据分析,并使用LSTM模型进行预测。 安装教程 pip install jupyter pip install numpy pandas matplotlib seaborn 使用说明 在项目路径下打开终端输入jupyter notebook就行
recommend-type

百问网linux桌面GUI,基于LVGL 8.x。.zip

百问网linux桌面GUI,基于LVGL 8.x。
recommend-type

基于Vue开发的XMall商城前台页面 PC端.zip

基于Vue开发的XMall商城前台页面 PC端.zip
recommend-type

2019年中国民航大学电子设计竞赛E题-自动导航运输车

2019年中国民航大学电子设计竞赛E题-自动导航运输车 全国大学生电子设计竞赛(National Undergraduate Electronics Design Contest),试题,解决方案及源码。计划或参加电赛的同学可以用来学习提升和参考
recommend-type

RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz

REALTEK 8188FTV 8188eus 8188etv linux驱动程序稳定版本, 支持AP,STA 以及AP+STA 共存模式。 稳定支持linux4.0以上内核。
recommend-type

管理建模和仿真的文件

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

:YOLOv1目标检测算法:实时目标检测的先驱,开启计算机视觉新篇章

![:YOLOv1目标检测算法:实时目标检测的先驱,开启计算机视觉新篇章](https://img-blog.csdnimg.cn/img_convert/69b98e1a619b1bb3c59cf98f4e397cd2.png) # 1. 目标检测算法概述 目标检测算法是一种计算机视觉技术,用于识别和定位图像或视频中的对象。它在各种应用中至关重要,例如自动驾驶、视频监控和医疗诊断。 目标检测算法通常分为两类:两阶段算法和单阶段算法。两阶段算法,如 R-CNN 和 Fast R-CNN,首先生成候选区域,然后对每个区域进行分类和边界框回归。单阶段算法,如 YOLO 和 SSD,一次性执行检
recommend-type

设计算法实现将单链表中数据逆置后输出。用C语言代码

如下所示: ```c #include <stdio.h> #include <stdlib.h> // 定义单链表节点结构体 struct node { int data; struct node *next; }; // 定义单链表逆置函数 struct node* reverse(struct node *head) { struct node *prev = NULL; struct node *curr = head; struct node *next; while (curr != NULL) { next
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。