0

这是我的一门课程的作业,我正在尝试创建一个程序,该程序将找到游戏“Knights Tour”的解决方案。在这里播放:https ://www.brainbashers.com/knight.asp

我让我的程序递归运行,直到找到解决方案。有64!不同的组合,其中大多数不是解决方案。当它抛出错误时 java.lang.StackOverflowError: null(在 java.util.ArrayLists 中)找到大约 30 种不同的组合后,就会出现问题。

问题出在 ArrayList 上,其他论坛说是“HashMash”占用了大量内存。我想知道我是否应该从 2 个数组重新开始?

import chn.util.*;
import java.util.*;
import java.awt.geom.*;
import apcslib.*;

public class Knight {
    public static void main(String[] args) {
        Solve Do = new Solve();
        Do.Boundries();
        Do.Display();
    }
}

class Solve {
    int[][] Grid = new int[8][8];
    int MaxR = Grid.length;
    int MaxC = Grid[1].length;
    ArrayList<Point> Point = new ArrayList<Point>(0);
    int nump = 0;
    int move = 1;
    int Random = 0;
    int Row = (int) (Math.random() * 7);
    int Col = (int) (Math.random() * 7);
    int Counter = 1;

    void Boundries() {
        // System.out.println("Cord: "+Row + ", "+Col);

        //Point0
        if (Row > 0 && Col > 1) {
            if (Grid[Row - 1][Col - 2] == 0) {
                Point.add(new Point(Row - 1, Col - 2));
                nump++;
            }
        }
        //Point1
        if (Row > 1 && Col > 0) {
            if (Grid[Row - 2][Col - 1] == 0) {
                Point.add(new Point(Row - 2, Col - 1));
                nump++;
            }
        }

        //Point2
        if (Row < 6 && Col > 0) {
            if (Grid[Row + 2][Col - 1] == 0) {
                Point.add(new Point(Row + 2, Col - 1));
                nump++;
            }
        }
        //Point3
        if (Row < 7 && Col > 1) {
            if (Grid[Row + 1][Col - 2] == 0) {
                Point.add(new Point(Row + 1, Col - 2));
                nump++;
            }
        }

        //Point5
        if (Row < 7 && Col < 6) {
            if (Grid[Row + 1][Col + 2] == 0) {
                Point.add(new Point(Row + 1, Col + 2));
                nump++;
            }
        }
        //Point6
        if (Row < 6 && Col < 7) {
            if (Grid[Row + 2][Col + 1] == 0) {
                Point.add(new Point(Row + 2, Col + 1));
                nump++;
            }
        }

        //Point7
        if (Row > 0 && Col < 6) {
            if (Grid[Row - 1][Col + 2] == 0) {
                Point.add(new Point(Row - 1, Col + 2));
                nump++;
            }
        }
        //Point8
        if (Row > 1 && Col < 7) {
            if (Grid[Row - 2][Col + 1] == 0) {
                Point.add(new Point(Row - 2, Col + 1));
                nump++;
            }
        }

        //for (int i = 0;i<nump;i++)
        // System.out.println(Point.get(i).xcord + ","+Point.get(i).ycord);

        if (nump != 0) {
            Random = (int) (Math.random() * nump);
            //for (int p=3;p<6;p++){
            int p = 0;
            int q = 7;
            for (int z = 1; z < nump; z++) {
                if (Point.get(z).xcord == p || Point.get(z).xcord == q || Point.get(z).ycord == p || Point.get(z).ycord == q) {
                    p++;
                    q--;
                    Random = z;
                }
            }
            //}
            nump = 0;
            Grid[Point.get(Random).xcord][Point.get(Random).ycord] = move;
            move++;
            Row = Point.get(Random).xcord;
            Col = Point.get(Random).ycord;
            Point = new ArrayList<Point>(0);
            Boundries();
        } else {
            System.out.println("\fNot a Solution: " + move + " moves");
            System.out.println("Amount of tries: " + Counter);
        }
        if (move < 64) {
            for (int t = 0; t < nump; t++)
                Point.remove(t);

            Display();
            Counter++;
            move = 0;
            nump = 0;
            Row = (int) (Math.random() * 7);
            Col = (int) (Math.random() * 7);
            //Point = new ArrayList<Point>(0);
            Grid = new int[8][8];
            Delay();
            Boundries();
        }
    }

    public static void gc() {
        System.gc();
    }

    void Delay() {
        try {
            Thread.sleep(100); //1000 milliseconds is one second.
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }

    void Display() {
        {
            int row, col;
            System.out.println();
            for (row = 0; row < 8; row++) {
                for (col = 0; col < 8; col++)
                    System.out.print((Format.left(Grid[row][col], 3) + " "));

                System.out.println();
            }
            System.out.println();
        }
    }
}

class Point {
    int xcord = 0;
    int ycord = 0;

    Point(int x, int y) {
        xcord = x;
        ycord = y;
    }
}
4

0 回答 0