Java 使用深度优先搜索算法的骑士之旅
我正在尝试使用DFS制作程序knight tour,但我无法解决此程序。。因为我总是有这样的消息错误 线程“AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException中出现异常:-1 位于java.util.ArrayList.elementData(ArrayList.java:371) 获取(ArrayList.java:384) 在KnightTour.processKnightTour(KnightTour.java:82) 我希望有人能帮助我Java 使用深度优先搜索算法的骑士之旅,java,depth-first-search,Java,Depth First Search,我正在尝试使用DFS制作程序knight tour,但我无法解决此程序。。因为我总是有这样的消息错误 线程“AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException中出现异常:-1 位于java.util.ArrayList.elementData(ArrayList.java:371) 获取(ArrayList.java:384) 在KnightTour.processKnightTour(KnightTour.java:82) 我希望
public void processKnightTour(int indexX, int indexY){
System.out.println(indexX);
System.out.println(indexY);
int[] x={2,2,-2,-2,1,1,-1,-1};
int[] y={1,-1,1,-1,2,-2,2,-2};
int countPath=0;
workList = new ArrayList();
node[indexX][indexY] = 1;
workList.add(new Coordinate(indexX, indexY));
current =(Coordinate) workList.get(workList.size()-1);
boolean statusChild;
while(node[current.row][current.column] != 64){
statusChild = false;
for(int loop=0; loop<8; loop++){
if(current.row+x[loop]>=0 && current.row+x[loop]<=7 && current.column+y[loop]>=0 && current.column+y[loop]<=7){
if(node[(current.row+x[loop])][(current.column+y[loop])]==0){
workList.add(new Coordinate(current.row+x[loop], current.column+y[loop], current));
statusChild = true;
}
}
}
if(statusChild == true){
workList.remove(workList.indexOf(current));
}else{
if(workList.size()-2 >= 0){
after = (Coordinate) workList.get(workList.size()-2);
if(current.nodeParent.equals(after.nodeParent)){
}else{
node[current.nodeParent.row][current.nodeParent.column] = 0;
}
}
node[current.row][current.column] = 0;
workList.remove(workList.size()-1);
}
current = (Coordinate) workList.get(workList.size()-1);
node[current.row][current.column] = (node[current.getParent().row][current.getParent().column])+1;
countPath++;
//System.out.println(countPath+", "+workList.size()+", "+node[current.column][current.row]);
}
}
public void processKnightTour(int indexX,int indexY){
系统输出打印项次(indexX);
系统输出打印LN(索引);
int[]x={2,2,-2,1,1,-1};
int[]y={1,-1,1,-1,2,-2,2};
int countPath=0;
workList=newarraylist();
节点[indexX][indexY]=1;
添加(新坐标(indexX,indexY));
当前=(坐标)workList.get(workList.size()-1);
布尔状态子;
while(节点[current.row][current.column]!=64){
statusChild=false;
for(int loop=0;loop=0&¤t.row+x[loop]=0&¤t.column+y[loop]=0){
after=(坐标)workList.get(workList.size()-2);
if(current.nodeParent.equals(在.nodeParent之后)){
}否则{
节点[current.nodeParent.row][current.nodeParent.column]=0;
}
}
节点【当前行】【当前列】=0;
workList.remove(workList.size()-1);
}
当前=(坐标)workList.get(workList.size()-1);
节点[current.row][current.column]=(节点[current.getParent().row][current.getParent().column])+1;
countPath++;
//System.out.println(countPath+,“+workList.size()+”,“+node[current.column][current.row]);
}
}
在这段代码中,几乎在代码末尾,您是:
- 首先在不知道列表中是否有元素的情况下删除元素
- 其次,您尝试从一个列表中
,该列表的大小可以(也将是)为0获取(worklist.size()-1)
while循环中有一些不太正确的地方。我没有清楚地看到意图,但您应该以某种方式确保
工作列表
的使用正确。谢谢您。。我的while循环有点不对劲..呵呵
workList.remove(workList.size()-1);
}
current = (Coordinate) workList.get(workList.size()-1);