A*算法求解8数码难题 要求Node类中有getHeuristicValue 方法 生成完整Java代码

时间: 2024-05-07 08:16:59 浏览: 11
以下是Java代码实现A*算法求解8数码难题,其中Node类中包含getHeuristicValue方法: ``` import java.util.PriorityQueue; import java.util.HashSet; import java.util.Set; public class EightPuzzleSolver { private int[][] goalState = {{1,2,3},{4,5,6},{7,8,0}}; private static class Node implements Comparable<Node> { int[][] state; // 当前状态 int fScore; // f(n) int gScore; // g(n) int hScore; // h(n) Node parent; // 父节点 public Node(int[][] state, int gScore, int hScore, Node parent) { this.state = state; this.gScore = gScore; this.hScore = hScore; this.fScore = gScore + hScore; this.parent = parent; } public int getHeuristicValue() { int h = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (state[i][j] != goalState[i][j]) { h++; } } } return h; } @Override public int compareTo(Node other) { return fScore - other.fScore; } @Override public boolean equals(Object other) { if (other instanceof Node) { Node o = (Node) other; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (state[i][j] != o.state[i][j]) { return false; } } } return true; } return false; } @Override public int hashCode() { int hash = 7; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { hash = 31 * hash + state[i][j]; } } return hash; } } public int[][] solve(int[][] initialState) { Node initialNode = new Node(initialState, 0, getHeuristicValue(initialState), null); Set<Node> visited = new HashSet<>(); PriorityQueue<Node> frontier = new PriorityQueue<>(); frontier.add(initialNode); while (!frontier.isEmpty()) { Node current = frontier.poll(); if (isGoalState(current.state)) { return current.state; // 找到解 } visited.add(current); for (Node neighbor : getNeighbors(current)) { if (!visited.contains(neighbor) && !frontier.contains(neighbor)) { frontier.add(neighbor); } else if (frontier.contains(neighbor)) { for (Node n : frontier) { if (n.equals(neighbor) && neighbor.gScore < n.gScore) { frontier.remove(n); frontier.add(neighbor); break; } } } } } return null; // 找不到解 } private boolean isGoalState(int[][] state) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (state[i][j] != goalState[i][j]) { return false; } } } return true; } private Set<Node> getNeighbors(Node node) { Set<Node> neighbors = new HashSet<>(); int[][] state = node.state; int blankI = -1, blankJ = -1; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (state[i][j] == 0) { blankI = i; blankJ = j; break; } } } if (blankI > 0) { int[][] newState = copy(state); swap(newState, blankI, blankJ, blankI - 1, blankJ); neighbors.add(new Node(newState, node.gScore + 1, getHeuristicValue(newState), node)); } if (blankI < 2) { int[][] newState = copy(state); swap(newState, blankI, blankJ, blankI + 1, blankJ); neighbors.add(new Node(newState, node.gScore + 1, getHeuristicValue(newState), node)); } if (blankJ > 0) { int[][] newState = copy(state); swap(newState, blankI, blankJ, blankI, blankJ - 1); neighbors.add(new Node(newState, node.gScore + 1, getHeuristicValue(newState), node)); } if (blankJ < 2) { int[][] newState = copy(state); swap(newState, blankI, blankJ, blankI, blankJ + 1); neighbors.add(new Node(newState, node.gScore + 1, getHeuristicValue(newState), node)); } return neighbors; } private static int[][] copy(int[][] state) { int[][] copy = new int[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { copy[i][j] = state[i][j]; } } return copy; } private static void swap(int[][] state, int i1, int j1, int i2, int j2) { int temp = state[i1][j1]; state[i1][j1] = state[i2][j2]; state[i2][j2] = temp; } private int getHeuristicValue(int[][] state) { int h = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (state[i][j] != goalState[i][j]) { h++; } } } return h; } public static void main(String[] args) { EightPuzzleSolver solver = new EightPuzzleSolver(); // 测试用例 int[][] initialState = {{1,2,3},{4,5,6},{7,0,8}}; int[][] solution = solver.solve(initialState); if (solution == null) { System.out.println("No solution found."); } else { System.out.println("Solution found:"); for (int i = 0; i < 3; i++) { System.out.println(solution[i][0] + " " + solution[i][1] + " " + solution[i][2]); } } } } ```

相关推荐

最新推荐

recommend-type

Java编程实现A*算法完整代码

主要介绍了Java编程实现A*算法完整代码,简单介绍了a星算法,然后分享了完整测试代码,具有一定借鉴价值,需要的朋友可以参考下。
recommend-type

【WHUT】*实验报告*《人工智能概论》课内实验:A*算法仿真实验

A*算法仿真实验 请下载并安装附件(虚拟实验软件-启发式搜索.rar)里的智能搜索算法教学实验系统,然后点击A*算法进行仿真实验。 实验要求如下: 1. 单击"A*算法介绍",回顾A*算法的基本原理。 2. 在"A*算法演示...
recommend-type

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

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

Python3 A*寻路算法实现方式

今天小编就为大家分享一篇Python3 A*寻路算法实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

A*算法解八数码问题(报告附录里有源代码),保准能运行

C++实现的A*算法解八数码问题,报告的附录里有源代码,在VC6.0的环境下开发运行的
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的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。