title: A Star algorithm in C implementation with arrays?
body: <p>I'm new here. I am trying to implement A-Star algorithm in C by myself. I don't know how to use Hashmaps or Lists (but i am open too learn as long as they are simple enough for me) so I use arrays. </p>
  
  <p>The problem is simple: There is a NxN array. You can either go up/down or left/right, no diagonial. Horizontal is better (less cost =5) than vertical movement (high cost=10). </p>
  
  <p>There are some obstacle-cells. Free cells are represented by number 0 in the NxN array, while obstacle cells with the number 9. The obstacle cells occur as a proportion of the area of the table (for example if the table is 10*10 and the independent possibility to have an obstacle in each cell is 0.1, there will be approximately 10 9's in the table.</p>
  
  <p>With the number 1 the starting point is represented and with 2 and 3 the two final goals to go, G1 and G2.</p>
  
  <p>I have tried this below: </p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(void) {
      //create a NxN array
  
      int N, sX, sY, g1X,g1Y,g2X,g2Y,i,j,w;
      double p;
      float r;
      printf("Give N\n");
      scanf("%d",&amp;N);
      printf("Give p\n");
      scanf("%lf",&amp;p);
      printf("Give S x k y\n");
      scanf("%d",&amp;sX);
      scanf("%d",&amp;sY);
      printf("Give G1 x &amp; y\n");
      scanf("%d",&amp;g1X);
      scanf("%d",&amp;g1Y);
      printf("Give G2 x &amp; y\n");
      scanf("%d",&amp;g2X);
      scanf("%d",&amp;g2Y);
  
      int table[N][N];
  
      for(i=0; i&lt;N; i++){
          for (j=0; j&lt;N; j++){
  
              r=(float)(rand() % 10)/10; // [0,1)
              //  printf("%f",&amp;r);
              if (sX==i &amp;&amp; sY==j){
                  table[i][j]=1;
                  //      printf("1");
              }
              else if(g1X==i &amp;&amp; g1Y==j){
                  table[i][j]=2;
                  //    printf("2");
              }
              else if( g2X==i &amp;&amp; g2Y==j){
                  table[i][j]=3;
                  //    printf("3");
              }
              else if (p&gt;=0 &amp;&amp; r&lt;=p){
                  table[i][j]=9;
                  //      printf("9");
              }
              else{
                  table[i][j]=0;
                  //  printf("0");
              }
  
  
              printf("%d ",table[i][j]);
  
          }
  
          printf("\n");
  
      }
  
      // Create the open list
  
  
      int cX=sX, cY=sY;
  
      while (cX!=g1X &amp;&amp; cY!=g1Y)
      {
          int openList[4][2];
          //TOP
          if(cX&gt;0 &amp;&amp; table[cX-1][cY]!=9){
              openList[0][0]=(cX-1);
              openList[0][1]=cY;
          }
          else{
              openList[0][0]=-1;
              openList[0][1]=-1;
          }
  
          //BOTTOM
          if(cX+1&lt;N &amp;&amp; table[cX+1][cY]!=9 ){
              openList[1][0]=(cX+1);
              openList[1][1]=cY;
          }
          else{
              openList[1][0]=-1;
              openList[1][1]=-1;
          }
          //RIGHT
          if(cY+1&lt;N &amp;&amp; table[cX][cY+1]!=9){
              openList[2][0]=cX;
              openList[2][1]=(cY+1);
          }
          else{
              openList[2][0]=-1;
              openList[2][1]=-1;
          }
          //LEFT
          if(cY&gt;0 &amp;&amp; table[cX][cY-1]!=9){
              openList[3][0]=cX;
              openList[3][1]=(cY-1);
          }
          else{
              openList[3][0]=-1;
              openList[3][1]=-1;
          }
  
          printf("Open List of current cell:%d,%d\n",&amp;cX, &amp;cY);
          for (i=0;i&lt;4;i++){
              printf("%d , %d\n",openList[i][0],openList[i][1]);
  
              cX=g1X; cY=g2Y;
          }
      }
      return 0;
  }
  </code></pre>
  
  <p>Questions:</p>
  
  <ol>
  <li><p>I know I didn't yet add the current cell in the open list. I should add it right?</p></li>
  <li><p>Both the openlist and the closed list should be a Hashmap?</p></li>
  <li><p>How do you think I should keep a connection with the parent of selected cell?</p></li>
  </ol>