2012-09-10 43 views
1

我正在学习如何给我们一个A *算法来查找路径,我希望看到这样做的最佳方式。这就是我想我想要有一个起点和终点,然后通过构建函数来构建迷宫,然后用产科填充它,然后将A *算法打印出路由表中的格式,基本上将0更改为一个3显示被采取的路径(产科将等于1)。这听起来像是一个好计划吗?迷宫与路径寻找算法

我有麻烦的是,我不知道放入产科阵列的最佳方式。 这是我到目前为止有:

public class Maze { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 


    //start and end points in the array 
    int startx = 115; 
    int starty = 655; 
    int endx = 380; 
    int endy = 560; 
    //number of collums and rows 
    int row = 700; 
    int col = 500; 
    //size of maze 
    int maze [][] = new int [row][col]; 

    makeMaze(row, col, maze); 
    printMaze(row, col, maze); 



} 

//fill mazz with 0 
    private static void makeMaze(int row, int col, int maze[][]) 
    { 
     //fill maze with 0 for initilization 
     for(int i = 0; i < row; i++) 
     { 
      for(int j = 0; j < col; j++) 
      { 
       maze[i][j] = 0; 
      } 

     } 
    } 
    //print out array/maze 
    private static void printMaze(int row, int col, int maze[][]) 
    { 
     //... Print array in rectangular form 
     for(int i = 0; i < row; i++) 
     { 
      for(int j = 0; j < col; j++) 
      { 
       System.out.print(" " + maze[i][j]); 
      } 
      System.out.println(""); 
     } 

    } 
    //fill the array with obsticals 
    private void makeObsticals() 
    { 
     //obstical 1 
     //this represent the corners of the object 
     int ob1Point1 [][] = new int [220][616]; 
     int ob1Point2 [][] = new int [220][666]; 
     int ob1Point3 [][] = new int [251][670]; 
     int ob1Point4 [][] = new int [272][647]; 

     //object 2 
     int ob2Point1 [][] = new int [341][655]; 
     int ob2Point2 [][] = new int [359][667]; 
     int ob2Point3 [][] = new int [374][651]; 
     int ob2Point4 [][] = new int [366][577]; 

     //obejct 3 
     int ob3Point1 [][] = new int [311][530]; 
     int ob3Point2 [][] = new int [311][559]; 
     int ob3Point3 [][] = new int [339][578]; 
     int ob3Point4 [][] = new int [361][560]; 
     int ob3Point5 [][] = new int [361][528]; 
     int ob3Point6 [][] = new int [113][516]; 

     //object 4 
     int ob4Point1 [][] = new int [105][628]; 
     int ob4Point2 [][] = new int [151][670]; 
     int ob4Point3 [][] = new int [180][629]; 
     int ob4Point4 [][] = new int [156][577]; 
     int ob4Point5 [][] = new int [113][587]; 

     //object 5 
     int ob5Point1 [][] = new int [118][517]; 
     int ob5Point2 [][] = new int [245][517]; 
     int ob5Point3 [][] = new int [245][577]; 
     int ob5Point4 [][] = new int [118][577]; 

     //object 6 
     int ob6Point1 [][] = new int [280][583]; 
     int ob6Point2 [][] = new int [333][583]; 
     int ob6Point3 [][] = new int [333][665]; 
     int ob6Point4 [][] = new int [280][665]; 

      //object 7 
     int ob7Point1 [][] = new int [252][594]; 
     int ob7Point2 [][] = new int [290][562]; 
     int ob7Point3 [][] = new int [264][538]; 

      //object 8 
     int ob8Point1 [][] = new int [198][635]; 
     int ob8Point2 [][] = new int [217][574]; 
     int ob8Point3 [][] = new int [182][574]; 


    } 
    //astar algorithum 
    private void findPath() 
    { 
    } 

}

感谢这个

+0

我不认为你真的意思*“产科”*。障碍可能? – assylias

+1

http://www.policyalmanac.org/games/aStarTutorial.htm – user902383

回答

1

对不起任何帮助,但我不明白为什么你们都宣称这么多的障碍二维数组。如你所说, 。 。 。

//obstacle1 
//this represent the corners of the object 
    int ob1Point1 [][] = new int [220][616]; 
    int ob1Point2 [][] = new int [220][666]; 
    int ob1Point3 [][] = new int [251][670]; 
    int ob1Point4 [][] = new int [272][647]; 

我想从上面的代码中你想的意思是(220616),(220666),(251670),(272647)是1个障碍物的角点。

如果是这样的话,我会建议不要采取4的二维阵列,但通过在障碍物迷宫覆盖[] []数组代替标记区由无穷即最高整数没有。 (让它考虑为10000)

和其他(x,y)位置,在maze [x] [y]中放置每个位置的启发式值(这意味着从该位置到达目的地(endx,endy) (x,y)位置)

然后应用A *算法从开始到结束。

+0

我之所以使用一堆数组是因为我能找出如何“构建”像我要取代的障碍的最佳方式带有1的障碍物区域中的所有数组值都代表不可通行的方式。我有点迷失在你无穷的意义上?那么我将如何为每个职位设置启发式价值?我只是把它标记为0,因为现在我知道A *需要知道到达目的地的费用是多少,但我在计算这个步骤时遇到了一些麻烦。 – MNM

+0

@MNM kk ..如果你没有错,你基本上想以某种方式代表你的迷宫中的障碍。为此,被障碍物覆盖的所有(x,y)位置使得迷宫[x] [y] = N(其中N在概念上是无穷的,并且在编程中我们不能写无穷大,所以我们将使N成为最大可能的整数没有。) 现在在你的程序中,让启发式值范围从(0到1000),然后使N =任何没有。大于1000让N = 10000 现在在程序中因为10000大于所有可能的启发式值A *算法将永远不会将该位置视为可能的路径以最短的成本到达目的地。 –

+0

我在构建阵列中的障碍时有点失落。这是我在想什么,但我并没有真正为(int i = 220; i <272; i ++) {0121},j <670; j ++) 迷宫[i] [j ] = 3; }} – MNM