2

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

Egan Wolf
  • 3,533
  • 1
  • 14
  • 29
KSP
  • 94
  • 1
  • 11
  • And, what does your code do? Which approach did you choose? For the future I recommend to comment and properly indent your code, currently I have no idea what's going on. Additionally: We don't know the test cases. So you'd have to somehow debug the code to check what exactly it is doing and where you may went wrong – Lino Jun 30 '21 at 14:54
  • @Lino Thank you so much for your input. This is my first time asking a question on StackOverflow. I'm working on making the edits you mentioned! Have a nice day! – KSP Jun 30 '21 at 15:22
  • Your code doesn't check at all what is to the left or above the node that is being processed. So it will fail for all maps where the optimal path after removing the wall goes left or up at some point. – Egan Wolf Jul 01 '21 at 11:40
  • @EganWolf I tried implementing it that way as well, but I still wound up at the same dead end. I'm not quite sure what to do. Do you have a particular solution in mind? – KSP Jul 01 '21 at 14:48
  • Maybe something like: initialize `dp` with very high number for each node and create a list of nodes to process. When you process a node check all 4 directions. If you found a lower value than the one that was assigned before add adjacent nodes to a list to process them again and see if their value can be lowered. – Egan Wolf Jul 01 '21 at 15:53
  • But I think it won't solve properly maps that start with a viable path that can be shorter after removing a wall. – Egan Wolf Jul 01 '21 at 15:57
  • @EganWolf Yeah, that approach doesn't work optimally for solutions where you might need to remove a wall. I keep hitting a dead end. I do, however have the solution to this question in Python, but I don't know python so I've not been able to draw inspiration from it. I ran the python code and it passes 5/5 test cases. So, here's the link, If it can be of any help.... https://blog.finxter.com/googles-foobar/ (Its the Level3 Challenge 1 question) – KSP Jul 02 '21 at 03:11
  • Does this answer your question? [Python breadth first shortest path for Google foo bar (prepare the bunnies escape)](https://stackoverflow.com/questions/46656412/python-breadth-first-shortest-path-for-google-foo-bar-prepare-the-bunnies-escap) – Anirudh Goel Dec 12 '21 at 21:24

0 Answers0