0
所以我让我的Astar程序运行,它运行4次失控6次,但它有时会给出两个不同的数组出界错误。Astar边界检查
我认为它是一个边界检查问题,但是我认为我的边界检查是正确的。 任何人都可以帮忙指出问题出在哪里?
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;
}
}
我改变了i> 0并把它放在前面,但没有区别。 并且i> 0和i-1> 0检查当前节点是否在映射内,并且如果邻居也在映射中,如果<0,则其为空。 – kkaul 2013-05-14 13:36:10
那么如果我 - 1> 0,不应该我> 0? – 2013-05-14 13:37:33
另外,你是否改变了所有类似的路线,或者你是否改变了所指出的路线? – 2013-05-14 13:37:55