这是我的一个课程的作业,我试图创建一个能找到游戏解决方案的程序" Knights Tour"。 在这里播放:https://www.brainbashers.com/knight.asp
我的程序以递归方式运行,直到找到解决方案。有64!
种不同的组合,其中大部分都不是解决方案。在它抛出错误时发现大约30种不同的组合后会出现问题
java.lang.StackOverflowError: null
(在java.util.ArrayLists中)。
问题在于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;
}
}