2013-05-14 88 views
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; 
    } 
} 

回答

0

if(Status[i-1][j] != FRONTIER && Status[i-1][j] != INTERIOR && i>0 && a)

你應該把i > 0i - 1之前讓短路防止i - 1被-1。

另外,i>0 && i-1>0只是j > 1

+0

我改變了i> 0並把它放在前面,但沒有區別。 並且i> 0和i-1> 0檢查當前節點是否在映射內,並且如果鄰居也在映射中,如果<0,則其爲空。 – kkaul 2013-05-14 13:36:10

+0

那麼如果我 - 1> 0,不應該我> 0? – 2013-05-14 13:37:33

+0

另外,你是否改變了所有類似的路線,或者你是否改變了所指出的路線? – 2013-05-14 13:37:55