title: Java 2D array shortest path from point A to point B with obstacles
body: <p>Objective: Finding the least amount of movement required to reach destination.</p>
  
  <p>Scenario: in a 2D Array 8*8 with elements such as the following:</p>
  
  <pre><code>*......B
  ........
  ****.**.
  .A....*.
  ........
  ....**..
  ........
  ....*...
  </code></pre>
  
  <p>where</p>
  
  <ul>
  <li>" A " represents the starting point.</li>
  <li>" B " represents the destination point.</li>
  <li>" * " represents an obstacle.</li>
  <li>" . " represents an empty cell.</li>
  </ul>
  
  <p>Currently I have done the following code:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.DataInputStream;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.io.InputStreamReader;
  
  class main
  {
      public static void main(String args[]) throws FileNotFoundException,IOException
      {   
          FileInputStream FS = new FileInputStream("path.in");
          DataInputStream DS = new DataInputStream(FS);
          BufferedReader buffer = new BufferedReader(new InputStreamReader(DS));
  
          String strLine = buffer.readLine();             
          int testCase = Integer.parseInt(strLine);
  
          int R,C;
  
          for(int i = 0;i &lt; testCase;i++)
          {           
  
              strLine = buffer.readLine();                    
              String input[] = strLine.split(" ");
  
              R = Integer.parseInt(input[0]);
              C = Integer.parseInt(input[1]);
  
              char[][] array = new char[R][C];
  
              int sCoordX = 0;
              int sCoordY = 0;
              int eCoordX = 0;
              int eCoordY = 0;
  
              for(int j = 0; j &lt; R ; j++)
              {
                  strLine = buffer.readLine();        
  
                  for(int k = 0;k &lt; C;k++)
                  {                               
                      array[j][k] = strLine.charAt(k);
                      if(array[j][k] == 'A')
                      {
                          sCoordX = j;
                          sCoordY = k;
                      }
  
                      if(array[j][k] == 'B')
                      {
                          eCoordX = j;
                          eCoordY = k;
                      }
                  }   
              }
  
              boolean reached = false;
              int counter = 0;
              int posX = sCoordX;
              int posY = sCoordY;
              while(!reached)
              {
                  if(array[posX][posY] == 'B')
                  {
                      reached = true;
                      System.out.println("You are in goal!");
                      System.out.println(array[posX][posY]);
                      System.out.println("Number of steps:"+counter);
                  }
  
                  if(!reached &amp;&amp; posX &gt; eCoordX)
                  {
                          posX--;
                          counter++;                  
                  }
                  else if(!reached &amp;&amp; posX &lt; eCoordX)
                  {
                      posX++;
                      counter++;                  
                  }
  
                  if(!reached &amp;&amp; posY &gt; eCoordY)
                  {
                      posY--;
                      counter++;          
                  }
                  else if(!reached &amp;&amp; posY &lt; eCoordY)
                  {
                      posY++;
                      counter++;              
                  }
              }
          }   
      }
  }
  </code></pre>
  
  <p>It does the job of finding the "shortest" amount of steps required to reach the destination however it will consider anything/obstacles as empty cells that it can move to.</p>
  
  <p>I'm currently unable to figure out a way to code it in a way that will allow it to recognize a proper decision for the next movement.</p>
  
  <p>I'm thinking of using array lists and some algorithm,however I tried reading some algorithms such as the <a href="http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm" rel="nofollow">Dijkstra's algorithm</a> but it seemed really confusing,can anybody help me understand it in a very easy manner to implement it in java?</p>
  
  <p>//-(sorry for my coding skills,I'm still a beginner)-</p>