启发式搜索算法a*搜索求解8数码问题 生成Java代码 简单实现不需要窗体 得出结果
时间: 2024-05-01 20:17:56 浏览: 130
Java-A*算法解决八数码问题算法源码
5星 · 资源好评率100%
好的,关于启发式搜索算法a*搜索求解8数码问题,以下是Java代码的简单实现:
```java
import java.util.*;
public class EightPuzzle {
// 棋盘维度
private static final int N = 3;
// 目标状态
private static final int[][] GOAL = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
// 初始状态
private int[][] board = new int[N][N];
// 空白格坐标
private int x, y;
public EightPuzzle(int[][] b) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
board[i][j] = b[i][j];
if (board[i][j] == 0) {
x = i;
y = j;
}
}
}
}
// 计算曼哈顿距离
private int manhattan() {
int distance = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
int targetNum = GOAL[i][j];
for (int k = 0; k < N; k++) {
for (int l = 0; l < N; l++) {
if (targetNum == board[k][l]) {
distance += Math.abs(i - k) + Math.abs(j - l);
}
}
}
}
}
return distance;
}
// 扩展状态
public List<EightPuzzle> expand() {
List<EightPuzzle> list = new ArrayList<>();
if (x > 0) {
int[][] newBoard = new int[N][N];
copy(newBoard, board);
int temp = newBoard[x][y];
newBoard[x][y] = newBoard[x - 1][y];
newBoard[x - 1][y] = temp;
list.add(new EightPuzzle(newBoard));
}
if (x < N - 1) {
int[][] newBoard = new int[N][N];
copy(newBoard, board);
int temp = newBoard[x][y];
newBoard[x][y] = newBoard[x + 1][y];
newBoard[x + 1][y] = temp;
list.add(new EightPuzzle(newBoard));
}
if (y > 0) {
int[][] newBoard = new int[N][N];
copy(newBoard, board);
int temp = newBoard[x][y];
newBoard[x][y] = newBoard[x][y - 1];
newBoard[x][y - 1] = temp;
list.add(new EightPuzzle(newBoard));
}
if (y < N - 1) {
int[][] newBoard = new int[N][N];
copy(newBoard, board);
int temp = newBoard[x][y];
newBoard[x][y] = newBoard[x][y + 1];
newBoard[x][y + 1] = temp;
list.add(new EightPuzzle(newBoard));
}
return list;
}
// 复制数组
private void copy(int[][] newBoard, int[][] board) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
newBoard[i][j] = board[i][j];
}
}
}
// 判断是否为目标状态
public boolean isGoal() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i][j] != GOAL[i][j]) {
return false;
}
}
}
return true;
}
// A*搜索
public static void AStarSearch(int[][] start) {
PriorityQueue<Node> pq = new PriorityQueue<>();
EightPuzzle root = new EightPuzzle(start);
Node rootNode = new Node(root, 0, root.manhattan(), null);
pq.offer(rootNode);
while (!pq.isEmpty()) {
Node node = pq.poll();
EightPuzzle cur = node.cur;
if (cur.isGoal()) {
List<EightPuzzle> path = new ArrayList<>();
while (node != null) {
path.add(node.cur);
node = node.parent;
}
Collections.reverse(path);
for (EightPuzzle p : path) {
p.printBoard();
}
return;
}
for (EightPuzzle p : cur.expand()) {
if (node.parent == null || !p.equals(node.parent.cur)) {
pq.offer(new Node(p, node.g + 1, p.manhattan(), node));
}
}
}
}
// 输出棋盘
public void printBoard() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
// 节点类
private static class Node implements Comparable<Node> {
EightPuzzle cur;
int g; // 到达当前状态所需步数
int h; // 启发函数估值
Node parent;
public Node(EightPuzzle cur, int g, int h, Node parent) {
this.cur = cur;
this.g = g;
this.h = h;
this.parent = parent;
}
public int f() {
return g + h;
}
@Override
public int compareTo(Node o) {
return this.f() - o.f();
}
}
public static void main(String[] args) {
int[][] start = { { 2, 8, 3 }, { 1, 6, 4 }, { 7, 0, 5 } };
AStarSearch(start);
}
}
```
这是一个简单的实现,不需要窗体展示,主要通过PriorityQueue实现了A*搜索算法。
阅读全文