现在我正在使用递归回溯,我的任务是在迷宫中找到最长的路径,质量显示为覆盖坐标的字段,文件中墙壁的坐标很痛。我做了一个解析器来解析输入文件并建造墙壁,但我也将这个坐标存储在对象类型 Coordinate 的数组中,以检查是否有可能在下一个移动下一个“蛇”字段,然后我创建了这个方法,现在我明白了,当我使用回溯时,我需要一个方法来从数组中删除最后一个坐标,我该怎么做?目标不是使用数组列表或链表只有数组!谢谢!
public class Coordinate {
int xCoord;
int yCoord;
Coordinate(int x,int y) {
this.xCoord=x;
this.yCoord=y;
}
public int getX() {
return this.xCoord;
}
public int getY() {
return this.yCoord;
}
public String toString() {
return this.xCoord + "," + this.yCoord;
}
}
和
public class Row {
static final int MAX_NUMBER_OF_COORD=1000;
Coordinate[] coordArray;
int numberOfElements;
Row(){
coordArray = new Coordinate[MAX_NUMBER_OF_COORD];
numberOfElements=0;
}
void add(Coordinate toAdd) {
coordArray[numberOfElements]=toAdd;
numberOfElements +=1;
}
boolean ifPossible(Coordinate c1){
for(int i=0;i<numberOfElements;i++){
if(coordArray[i].xCoord==c1.xCoord && coordArray[i].yCoord==c1.yCoord){
return false;
}
}
return true;
}
}
原文由 Andre Liberty 发布,翻译遵循 CC BY-SA 4.0 许可协议
由于在 Java 中,数组是不可调整大小的,因此您必须将所有内容复制到一个新的、更短的数组中。