GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

Find the lexicographical Kth shortest path in 2d Plane .

Bob is standing at cell (0,0) and he wants to go to cell (x, y) in the smallest number of moves. But there are so many possible shortest path and in some of the path there are dangerous drag. Knows that if he uses the lexicographically kth shortest path, he will be able to avoid the dragons!

All possible shortest ways consist of some horizontal and some vertical moves, lets denote the moves by H and V. A possible way to go from (0,0) to (2,2) is HVHV, that means he first made a horizontal move , Then a vertical move, then a horizontal move again and finally a vertical move. HVVH can be another possible way but HVHV is lexicographically smaller that HVVH.

Given the value of k find the lexicographically kth path to go from (0,0) to (x, y) using smallest number of moves. Note that k is numbered from 0 to P-1 where P is number of possible paths. For the example above HHVV is the lexicograpically 0th path.

Input Parameters:

You need to complete a function gridLand. If will take an array of string inp as parameter. Each element of the inp will represent a single case in the form "xy k".

Return Value:

Return an array of string, ith element of the array should contain answer of ith test case.

Constraints

1 <= | inp | <= 100000

1 <= x <= 10

1 <= y <= 10

0 <= k <number of paths

€ € 垎 垎 垎 垎 垎 垎 垎 垎

Sample Input

Inp = {"2 2 2", "2 2 3",}

Sample Output

{"HVVH", "VHHV"}

Explanation

All the paths of going to (2,2) from (0,0) in lexicographically increasing order:

0. HHVV

1. HVHV

2. HVVH

3. VHHV

4. VHVH

5. VVHH

(0,0) to (x, y), will produce a path string, the path must be x H and y V composition, give you a K , In all paths which follow the alphabetical order of the first K path

asked Dec 31, 2016 by MANOJ REDDY

1 Answer

public class Solution {
    public String[] helpingBob(String[] destination) {
        String[] result = new String[destination.length];
        for(int i=0;i<destination.length;i++)
        {
            
                int source = Character.getNumericValue(destination[i].charAt(0));
                int dest = Character.getNumericValue(destination[i].charAt(1));
                int k = Character.getNumericValue(destination[i].charAt(3));
                result[i] = shortestPaths(source,dest,k);
                
            
        }
        return result;
        
    }
    
    public static String shortestPaths(int source, int destination,int k)
    {
        
        String[][] result = new String[source+1][destination+1];
        result[0][0] = "";
        for(int i=1;i<=source;i++)
        {
            result[i][0] = result[i-1][0] + "H";
            
        }
        
        for(int j=1;j<=destination;j++)
        {
            result[0][j] = result[0][j-1] + "V";
            
        }
        for(int i=1;i<=source;i++)
        {
            for(int j=1;j<=destination;j++)
            {
                result[i][j] = shortestPathsUtil(result[i][j-1],"V") + "," + shortestPathsUtil(result[i-1][j],"H");
                
            }
        }
        
        System.out.println(result[source][destination]);
        String[] path = result[source][destination].split(",");
        Arrays.sort(path);
        
        return path[k];
    }
    
        public static String shortestPathsUtil(String horizontal, String v)
    {
        
        String[] horizon = horizontal.split(",");
        
        String path="";
        if(v.equals("H"))
        {
            for(int i=0;i<horizon.length;i++)
            {
                horizon[i] = horizon[i] + "H";
                if(path.length() == 0)
                {
                    path = path + horizon[i];
                }
                else
                {
                    path = path + "," + horizon[i];
                }
                
                
            }
        }
        if(v.equals("V"))
        {
            for(int i=0;i<horizon.length;i++)
            {
                horizon[i] = horizon[i] + "V";
                if(path.length() == 0)
                {
                    path = path + horizon[i];
                }
                else
                {
                    path = path + "," + horizon[i];
                }
                
                
            }
        }
        
        return path;
    }
}

answered Jan 11 by MANOJ REDDY
...