首先,有很多代码,因为我真的不知道我的问题在哪里,我道歉!

我正在制作一个解决不同尺寸拼图的程序,例如(3x3,0-8)零表示一个空白图块,目标是移动空白图块直到达到目标状态 . 目前我正在使用深度优先搜索来解决难题 . Netbeans在运行程序时返回“OutOfMemory”错误,但如果我更改目标状态,则只需要一次移动即可完成显示解决方案 . 下面是我的代码,我只添加了一些类,因为我不希望这篇文章长得太荒谬 . 如果您需要其他课程,请告诉我

EDIT: Inserted wrong code for board class

Depth First Search Class

package npuzzle;

import java.util.ArrayList;
import java.util.Stack;

public class DFSearch {

public static void search(int[][] initial, int[][] goal, int rows, int columns)
{
    Board board = new Board(initial, goal, rows, columns, "");
    Node root = new Node(board);
    Stack<Node> stack = new Stack<Node>();

    stack.add(root);

    performSearch(stack, board);
}

 public static void performSearch(Stack<Node> s, Board board)
{
    int searchCount = 1;

    while (!s.isEmpty())
    {
        Node tNode = (Node) s.pop();

        //if not goal state
        if (!tNode.getCurrentState().isGoalState())
        {
            ArrayList<State> tChildren = tNode.getCurrentState().gChildren();

            for (int i = 0; i < tChildren.size(); i++)
            {
                Node newNode = new Node(tNode, tChildren.get(i), tNode.getGCost() + tChildren.get(i).findCost(), 0);

                if(!isRepeatedState(newNode))
                {
                    s.add(newNode);
                }
            }
            searchCount++;
        }
        else
        {                
            tNode.getCurrentState().printState();
            System.exit(0);
            //PRINTING OF DIRECTIONS
        }
    }

    System.out.println("Error! No solution found!");
}

private static boolean isRepeatedState(Node c)
{
    boolean returnVal = false;
    Node checkNode = c;

    while(c.getParent() != null && !returnVal)
    {
        if (c.getParent().getCurrentState().equals(checkNode.getCurrentState()))
        {
            returnVal = true;
        }

        c = c.getParent();
    }

    return returnVal;
}

}

Board Class

package npuzzle;

import java.util.ArrayList;
import java.util.Arrays;

public class Board implements State
{
private int PUZZLE_SIZE = 0;
private int outOfPlace = 0;

private int rows;
private int columns;

private int[][] GOAL;

private int[][] currentBoard;

private String directions = "";

public Board(int[][] initial, int[][] goal, int N, int M, String direction)
{
    currentBoard = initial;
    GOAL = goal;

    rows = N;
    columns = M;

    PUZZLE_SIZE = rows*columns;

    directions = direction;

    setOutOfPlace();
}

@Override
public boolean isGoalState() {

    if (Arrays.deepEquals(currentBoard, GOAL))
    {
        return true;
    }

    return false;
}

@Override
public ArrayList<State> gChildren() {

    ArrayList<State> children = new ArrayList<State>();
    int[] blanktile = getBlankTile();
    int[] newblanktile = Arrays.copyOf(blanktile, blanktile.length);

    if (blanktile[0] != 0) {
        newblanktile[0]--;
        Swap(newblanktile, blanktile, children, "up");
        newblanktile =  Arrays.copyOf(blanktile, blanktile.length);
    }

    if (blanktile[1] != 0) {
        newblanktile[1]--;
        Swap(newblanktile, blanktile, children, "left");
        newblanktile =  Arrays.copyOf(blanktile, blanktile.length);
    }

    if (blanktile[0] != (this.rows - 1)) {
        newblanktile[0]++;
        Swap(newblanktile, blanktile, children, "down");
        newblanktile =  Arrays.copyOf(blanktile, blanktile.length);
    }

    if (blanktile[1] != (this.columns - 1)) {
        newblanktile[1]++;
        Swap(newblanktile, blanktile, children, "right");
        newblanktile =  Arrays.copyOf(blanktile, blanktile.length);
    }

    return children;
}

@Override
public double findCost() {
    return 1;
}

@Override
public void printState() {
    System.out.println(directions);
}

@Override
public boolean equals(State s) {

    if (Arrays.deepEquals(currentBoard, ((Board) s).getCurrentBoard()))
    {
        return true;
    }
    else
        return false;
}

private void setOutOfPlace() {

    int i = 0;
    int j = -1;

    do 
    {
        if (j == (columns - 1)) {j = 0; i++;}
        else {j++;}

        if (currentBoard[i][j] != GOAL[i][j])
        {
            outOfPlace++;
        }

    } while (((i+1)*(j+1)) < PUZZLE_SIZE);
}

private int[] getBlankTile()
{
    int i = 0;
    int j = -1;

    int[] blanktile = {0,0};

    do 
    {
        if (j == (columns - 1)) {j = 0; i++;}
        else {j++;}

        if (currentBoard[i][j] == 0) {
            blanktile[0] = i;
            blanktile[1] = j;
        }

    } while (((i+1)*(j+1)) < PUZZLE_SIZE);
    return blanktile;
}

public int getOutOfPlace()
{
    return outOfPlace;
}

private int[][] copyBoard(int[][] state)
{
    int[][] returnArray = new int[rows][columns];
    for (int i = 0, j = 0; i*j < PUZZLE_SIZE; i++, j++)
    {
        returnArray[i] = Arrays.copyOf(state[i], state[i].length);
    }
    return returnArray;
}

private void Swap(int[] nbt, int[] bt, ArrayList<State> children, String direction) {

    int[][] cpy = copyBoard(currentBoard);
    int temp = cpy[nbt[0]][nbt[1]];
    cpy[nbt[0]][nbt[1]] = currentBoard[bt[0]][bt[1]];
    cpy[bt[0]][bt[1]] = temp;
    children.add(new Board(cpy, this.getGOAL(), this.getRows(), this.getColumns(), (this.getDirections() + direction + ", ")));
}

 public int getPUZZLE_SIZE() {
    return PUZZLE_SIZE;
}

public int getRows() {
    return rows;
}

public int getColumns() {
    return columns;
}

public int[][] getGOAL() {
    return GOAL;
}

public int[][] getCurrentBoard()
{
    return currentBoard;
}

public String getDirections()
{
    return directions;
}

}