'Google foobar challenge (Prepare the bunnies escape)

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! I used a minimum cost path algorithm to do it

This is the challenge: 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.

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).

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.

And this is the code I've written

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+","+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<m;i++) 
        {
            for(int j=0;j<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>=0;i--) 
        {
            for(int j=dp[0].length-1;j>=0;j--) 
            {
                if(i==dp.length-1 && 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>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("Distance: "+solution(arr));
    }
}

and it passes 3/5 test cases

test cases

I know two of the test cases, please find them here.

two of the test cases

but the other three test cases are given at random.

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.

Edit: I've figured out that for most inputs, the output tends to be n+m-1.

I've also found out a case where my code doesn't work. Input:

{{0,0,1,1},
 {1,0,0,0},
 {0,0,1,1},
 {0,1,1,1},
 {0,1,1,1},
 {0,1,0,0}}

Expected output = 11

Output from above code = 1008



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source