title: Google foobar challenge (Prepare the bunnies escape)
body: <p><em>I recently received an invitation to a google foobar challenge
  I am currently on level 3. I've written a code which passes 3/5 test cases and I can't seem to figure out where I went wrong!</em>
  I used a minimum cost path algorithm to do it</p>
  <p><strong>This is the challenge:</strong>
  You're awfully close to destroying the LAMBCHOP doomsday device and freeing Commander Lambda's bunny workers, but once they're free of the work duties the bunnies are going to need to escape Lambda's space station via the escape pods as quickly as possible. Unfortunately, the halls of the space station are a maze of corridors and dead ends that will be a deathtrap for the escaping bunnies. Fortunately, Commander Lambda has put you in charge of a remodeling project that will give you the opportunity to make things a little easier for the bunnies. Unfortunately (again), you can't just remove all obstacles between the bunnies and the escape pods - at most you can remove one wall per escape pod path, both to maintain structural integrity of the station and to avoid arousing Commander Lambda's suspicions.</p>
  <p>You have maps of parts of the space station, each starting at a work area exit and ending at the door to an escape pod. The map is represented as a matrix of 0s and 1s, where 0s are passable space and 1s are impassable walls. The door out of the station is at the top left (0,0) and the door into an escape pod is at the bottom right (w-1,h-1).</p>
  <p>Write a function solution(map) that generates the length of the shortest path from the station door to the escape pod, where you are allowed to remove one wall as part of your remodeling plans. The path length is the total number of nodes you pass through, counting both the entrance and exit nodes. The starting and ending positions are always passable (0). The map will always be solvable, though you may or may not need to remove a wall. The height and width of the map can be from 2 to 20. Moves can only be made in cardinal directions; no diagonal moves are allowed.</p>
  <p>And this is the code I've written</p>
  <pre><code>public class Main 
  {
      //function to return shortest path
      public static int solution(int[][] map) 
      {
          int n=map[0].length;
          int m=map.length;
          System.out.println(n+&quot;,&quot;+m);
          //assigning weights to the array
          //such that the blocked walls have a large weight of 1000
          int w[][]=new int[m][n];
          for(int i=0;i&lt;m;i++) 
          {
              for(int j=0;j&lt;n;j++) 
              {
                  if(map[i][j]==0)
                      w[i][j]=1;
                  else if(map[i][j]==1)
                      w[i][j]=1000;
              }
          }
      
          int[][] dp=new int[m][n];
          for(int i=dp.length-1;i&gt;=0;i--) 
          {
              for(int j=dp[0].length-1;j&gt;=0;j--) 
              {
                  if(i==dp.length-1 &amp;&amp; j==dp[0].length-1)
                  {
                      dp[i][j]=w[i][j];
                  }
                  else if(i==dp.length-1)
                  {
                      dp[i][j]=dp[i][j+1]+w[i][j];
                  }
                  else if(j==dp[0].length-1) 
                  {
                      dp[i][j]=dp[i+1][j]+w[i][j];
                  }
                  else 
                  {
                      dp[i][j]=Math.min(dp[i+1][j], dp[i][j+1])+w[i][j];
                  }
              }
          }
          int distance=dp[0][0];
          int d;
          if(distance&gt;1000) 
          {
              d= distance-999;
              return d;
          }
          else
              return distance;
      }
      public static void main(String[] args)throws Exception
      {
          int arr[][]= {{0,1,1},{0,0,0},{1,1,0},{1,1,0}};
          System.out.println(&quot;Distance: &quot;+solution(arr));
      }
  }
  </code></pre>
  <p>and it passes 3/5 test cases</p>
  <p><img src="https://i.stack.imgur.com/ogvHP.png" alt="test cases" /></p>
  <p>I know two of the test cases, please find them here.</p>
  <p><img src="https://i.stack.imgur.com/aqZjp.png" alt="two of the test cases" /></p>
  <p>but the other three test cases are given at random.</p>
  <p>I have made an array w (weight) such that 1's translate to passable paths and 1000's translate to impassable paths so that they're less favored by the algorithm.
  I've used a simplified Dijkstra's algorithm in this code.</p>
  <p>Edit: I've figured out that for most inputs, the output tends to be n+m-1.</p>
  <p>I've also found out a case where my code doesn't work.
  Input:</p>
  <pre><code>{{0,0,1,1},
   {1,0,0,0},
   {0,0,1,1},
   {0,1,1,1},
   {0,1,1,1},
   {0,1,0,0}}
  </code></pre>
  <p>Expected output = 11</p>
  <p>Output from above code = 1008</p>