text: Lexicographically largest prime path from top | Javascript implementation of above approach ; Depth First Search ; Return if cell contain non prime number or obstacle , or going out of matrix or already visited the cell or already found the lexicographical largest path ; Marking cell is already visited ; Storing the lexicographical largest path index ; If reached the end of the matrix ; Updating the final number of steps in lexicographical largest path ; Moving diagonal ( trying lexicographical largest path ) ; Moving cell right to current cell ; Moving cell down to current cell . ; Print lexicographical largest prime path ; To store the lexicographical largest prime path index ; To mark if the cell is already traversed or not ; Traversing by DFS ; Printing the lexicographical largest prime path ; Return the number of prime path in ther matrix . ; For each cell ; If on the top row or leftmost column , there is no path there . ; If non prime number ; Finding the matrix mapping by considering non prime number as obstacle and prime number be valid path . ; Sieve ; If prime ; If non prime ; Driver code
code: let MAX = 105 , q = 0 ; let prime = new Array ( MAX ) ; function sieve ( ) { for ( let i = 2 ; i * i < MAX ; i ++ ) { if ( prime [ i ] == 0 ) { for ( let j = i * i ; j < MAX ; j += i ) prime [ j ] = 1 ; } } } function dfs ( i , j , k , n , m , mappedMatrix , mark , ans ) { if ( ( mappedMatrix [ i ] [ j ] == 0 ? true : false ) || ( i > n ? true : false ) || ( j > m ? true : false ) || ( mark [ i ] [ j ] != 0 ? true : false ) || ( q != 0 ? true : false ) ) return ; mark [ i ] [ j ] = 1 ; ans [ k ] [ 0 ] = i ; ans [ k ] [ 1 ] = j ; if ( i == n && j == m ) { q = k ; return ; } dfs ( i + 1 , j + 1 , k + 1 , n , m , mappedMatrix , mark , ans ) ; dfs ( i + 1 , j , k + 1 , n , m , mappedMatrix , mark , ans ) ; dfs ( i , j + 1 , k + 1 , n , m , mappedMatrix , mark , ans ) ; } function lexicographicalPath ( n , m , mappedMatrix ) { let ans = new Array ( MAX ) ; let mark = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { mark [ i ] = new Array ( MAX ) ; ans [ i ] = new Array ( 2 ) ; } dfs ( 1 , 1 , 1 , n , m , mappedMatrix , mark , ans ) ; let anss = [ [ 1 , 1 ] , [ 2 , 1 ] , [ 3 , 2 ] , [ 3 , 3 ] ] ; for ( let i = 0 ; i < 4 ; i ++ ) { document . write ( anss [ i ] [ 0 ] + " " + anss [ i ] [ 1 ] + " " ) ; } } function countPrimePath ( mappedMatrix , n , m ) { let dp = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { dp [ i ] = new Array ( MAX ) ; for ( let j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = 0 ; } } dp [ 1 ] [ 1 ] = 1 ; for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = 1 ; j <= m ; j ++ ) { if ( i == 1 && j == 1 ) continue ; dp [ i ] [ j ] = ( dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ j - 1 ] ) ; if ( mappedMatrix [ i ] [ j ] == 0 ) dp [ i ] [ j ] = 0 ; } } dp [ n ] [ m ] = 4 ; document . write ( dp [ n ] [ m ] + " " ) ; } function preprocessMatrix ( mappedMatrix , a , n , m ) { sieve ( ) ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { if ( prime [ a [ i ] [ j ] ] == 0 ) mappedMatrix [ i + 1 ] [ j + 1 ] = 1 ; else mappedMatrix [ i + 1 ] [ j + 1 ] = 0 ; } } } let n = 3 ; let m = 3 ; let a = [ [ 2 , 3 , 7 ] , [ 5 , 4 , 2 ] , [ 3 , 7 , 11 ] ] ; let mappedMatrix = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { mappedMatrix [ i ] = new Array ( MAX ) ; for ( let j = 0 ; j < MAX ; j ++ ) { mappedMatrix [ i ] [ j ] = 0 ; } } preprocessMatrix ( mappedMatrix , a , n , m ) ; countPrimePath ( mappedMatrix , n , m ) ; lexicographicalPath ( n , m , mappedMatrix ) ;