编写八数码问题的启发式搜索的Java代码。以下是一个示例代码:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.PriorityQueue;
public class EightPuzzle {
private static final int[][] GOAL_STATE = {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}};
private static int[][] initialState = {{5, 1, 3}, {4, 0, 2}, {7, 8, 6}};
public static void main(String[] args) {
PriorityQueue<Node> openList = new PriorityQueue<>((a, b) -> (a.f - b.f));
HashSet<Node> closedList = new HashSet<>();
Node startNode = new Node(initialState, 0, 0);
openList.add(startNode);
while (!openList.isEmpty()) {
Node currentNode = openList.poll();
closedList.add(currentNode);
if (isGoalState(currentNode.state)) {
System.out.println("Goal state reached!");
printPath(currentNode);
return;
}
ArrayList<Node> neighbors = generateNeighbors(currentNode);
for (Node neighbor : neighbors) {
if (closedList.contains(neighbor)) {
continue;
}
int g = currentNode.g + 1;
int h = calculateHeuristic(neighbor.state);
int f = g + h;
if (openList.contains(neighbor)) {
Node existingNode = getNodeFromOpenList(neighbor, openList);
if (existingNode.f <= f) {
continue;
}
openList.remove(existingNode);
}
neighbor.g = g;
neighbor.h = h;
neighbor.f = f;
neighbor.parent = currentNode;
openList.add(neighbor);
}
}
System.out.println("Goal state not reachable!");
}
private static boolean isGoalState(int[][] state) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (state[i][j] != GOAL_STATE[i][j]) {
return false;
}
}
}
return true;
}
private static ArrayList<Node> generateNeighbors(Node node) {
ArrayList<Node> neighbors = new ArrayList<>();
int[][] state = node.state;
int row = 0, col = 0;
// Find the position of the blank tile
outerloop:
for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {
if (state[row][col] == 0) {
break outerloop;
}
}
}
// Generate neighbors by moving the blank tile
// Up
if (row > 0) {
int[][] newState = cloneState(state);
swap(newState, row, col, row - 1, col);
neighbors.add(new Node(newState, 0, 0));
}
// Down
if (row < 2) {
int[][] newState = cloneState(state);
swap(newState, row, col, row + 1, col);
neighbors.add(new Node(newState, 0, 0));
}
// Left
if (col > 0) {
int[][] newState = cloneState(state);
swap(newState, row, col, row, col - 1);
neighbors.add(new Node(newState, 0, 0));
}
// Right
if (col < 2) {
int[][] newState = cloneState(state);
swap(newState, row, col, row, col + 1);
neighbors.add(new Node(newState, 0, 0));
}
return neighbors;
}
private static int[][] cloneState(int[][] state) {
int[][] newState = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
newState[i][j] = state[i][j];
}
}
return newState;
}
private static void swap(int[][] state, int i, int j, int x, int y) {
int temp = state[i][j];
state[i][j] = state[x][y];
state[x][y] = temp;
}
private static int calculateHeuristic(int[][] state) {
int heuristic = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (state[i][j] != 0 && state[i][j] != GOAL_STATE[i][j]) {
heuristic++;
}
}
}
return heuristic;
}
private static Node getNodeFromOpenList(Node targetNode, PriorityQueue<Node> openList) {
for (Node node : openList) {
if (node.equals(targetNode)) {
return node;
}
}
return null;
}
private static void printPath(Node node) {
if (node.parent != null) {
printPath(node.parent);
}
printState(node.state);
System.out.println();
}
private static void printState(int[][] state) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(state[i][j] + " ");
}
System.out.println();
}
}
static class Node {
int[][] state;
int g;
int h;
int f;
Node parent;
Node(int[][] state, int g, int h) {
this.state = state;
this.g = g;
this.h = h;
this.f = g + h;
parent = null;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Node node = (Node) obj;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (state[i][j] != node.state[i][j]) {
return false;
}
}
}
return true;
}
@Override
public int hashCode() {
int result = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result = result * 31 + state[i][j];
}
}
return result;
}
}
}
请注意,此示例代码中使用的初始状态是{{5, 1, 3}, {4, 0, 2}, {7, 8, 6}}
,你可以根据需要进行更改。启发式函数使用的是错误放置的方块数量,如果你想使用其他启发式函数,可以在calculateHeuristic
方法中修改。 此外,代码中还提供了一个printState
方法来打印状态矩阵以及最终搜索路径的printPath
方法。你可以根据需要进行修改和扩展。
希望这段代码能帮到你