所以我让我的 Astar 程序运行,它运行了 4 次 6 次,但是它有时会给出两个不同的数组越界错误。
我认为这是边界检查的问题,但是我认为我的边界检查是正确的。谁能帮忙指出问题出在哪里?
public class myastar extends astar
{
public myastar(int r, int c) { super(r,c); }
static final int NEW =0;
static final int INTERIOR =1;
static final int FRONTIER =2;
int[][] Status = new int[ROWS][COLS]; //initialize a new matrix of all 0s--keep track of status of node
public coord search(int sy, int sx, int ty, int tx)
{
coord start = new coord (sy, sx);
start.dist = 0;
start.cost=0;
coord current = null;
//create frontier heap
pheap<coord> Frontier = new pheap<coord>(new coord[1000]);
//Insert start into frontier
Frontier.insert(start);
//Status
Status [sy][sx] = FRONTIER;
//Boolean value of when to stop while
boolean stop = false;
//cost
int []cost = {1, 0, 0, 5};
while(!stop && Frontier.size() != 0)
{
current = Frontier.deletetop();
Status[current.y][current.x] = INTERIOR;
int i = current.y;
int j = current.x;
coord neighborn = new coord(i-1, j); //NORTH
coord neighborw = new coord(i, j-1); //WEST
coord neighbore = new coord(i, j+1); //EAST
coord neighbors = new coord(i+1, j); //SOUTH
if (i>0 && i-1>0){
//North
neighborn.dist = current.dist + 1;
neighborn.cost = neighborn.dist + cost[M[i-1][j]] + ddist(i-1, j, ty, tx);
}
if (j>0 && j-1>0){
//West
neighborw.dist = current.dist + 1;
neighborw.cost = neighborw.dist + cost[M[i][j-1]] + ddist(i, j-1, ty, tx);
}
if (j<COLS && j+1<COLS){
//East
neighbore.dist = current.dist + 1;
neighbore.cost = neighbore.dist+ cost[M[i][j+1]] + ddist(i, j+1, ty, tx);
}
if (i<ROWS && i+1<ROWS ){
//South
neighbors.dist = current.dist + 1;
neighbors.cost = neighbors.dist+ cost[M[i+1][j]] + ddist(i+1, j, ty, tx);
}
boolean a = true;
if(i<=0){a=false;}
boolean b = false;
boolean c = false;
boolean d = false;
if(neighborw.compareTo(neighborn) > 0){b=true;a=false;}
if(j<=0){b=false;}
if(neighbore.compareTo(neighborw) > 0){c=true;b=false;}
if(i>=COLS){c=false;}
if(neighbors.compareTo(neighbore) > 0){d=true;c=false;}
if(j>=ROWS){d=false;}
if(Status[i-1][j] != FRONTIER && Status[i-1][j] != INTERIOR && i>0 && a)// M[i-1][j] == OPEN && i>0 && a)
{
Frontier.insert(neighborn); //NORTH
neighborn.prev = current;
Status[i-1][j] = FRONTIER;
System.out.println("north");
}
if(Status[i][j-1] != FRONTIER && Status[i][j-1] != INTERIOR && j>0 && b)// M[i][j-1] == OPEN && j>0 && b)
{
Frontier.insert(neighborw); //WEST
neighborw.prev = current;
Status[i][j-1] = FRONTIER;
System.out.println("west");
}
if(Status[i][j+1] != FRONTIER && Status[i][j+1] != INTERIOR && j<COLS && c)// M[i][j+1] == OPEN && j<COLS && c)
{
Frontier.insert(neighbore); //EAST
neighbore.prev = current;
Status[i][j+1] = FRONTIER;
System.out.println("east");
}
if(Status[i+1][j] != FRONTIER && Status[i+1][j] != INTERIOR && i<ROWS && d)// M[i+1][j] == OPEN && i<ROWS && d)
{
Frontier.insert(neighbors); //SOUTH
neighbors.prev = current;
Status[i+1][j] = FRONTIER;
System.out.println("south");
}
if(i==ty && j == tx){stop = true;}
}
return current;
}
}