inputs: "Lexicographically largest prime path from top | C ++ 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 count the number of step in 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"
Solution in C++:
targets: #include <bits/stdc++.h>
using namespace std ; #define MAX  105
void sieve ( int prime [ ] ) { for ( int i = 2 ; i * i <= MAX ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = i * i ; j <= MAX ; j += i ) prime [ j ] = 1 ; } } } void dfs ( int i , int j , int k , int * q , int n , int m , int mappedMatrix [ ] [ MAX ] , int mark [ ] [ MAX ] , pair < int , int > ans [ ] ) { if ( mappedMatrix [ i ] [ j ] == 0 || i > n || j > m || mark [ i ] [ j ] || ( * q ) ) return ; mark [ i ] [ j ] = 1 ; ans [ k ] = make_pair ( i , j ) ; if ( i == n && j == m ) { ( * q ) = k ; return ; } dfs ( i + 1 , j + 1 , k + 1 , q , n , m , mappedMatrix , mark , ans ) ; dfs ( i + 1 , j , k + 1 , q , n , m , mappedMatrix , mark , ans ) ; dfs ( i , j + 1 , k + 1 , q , n , m , mappedMatrix , mark , ans ) ; } void lexicographicalPath ( int n , int m , int mappedMatrix [ ] [ MAX ] ) { int q = 0 ; pair < int , int > ans [ MAX ] ; int mark [ MAX ] [ MAX ] ; dfs ( 1 , 1 , 1 , & q , n , m , mappedMatrix , mark , ans ) ; for ( int i = 1 ; i <= q ; i ++ ) cout << ans [ i ] . first << " ▁ " << ans [ i ] . second << " 
 " ; } void countPrimePath ( int mappedMatrix [ ] [ MAX ] , int n , int m ) { int dp [ MAX ] [ MAX ] = { 0 } ; dp [ 1 ] [ 1 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int 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 ; } } cout << dp [ n ] [ m ] << " 
 " ; } void preprocessMatrix ( int mappedMatrix [ ] [ MAX ] , int a [ ] [ MAX ] , int n , int m ) { int prime [ MAX ] ; sieve ( prime ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( prime [ a [ i ] [ j ] ] == 0 ) mappedMatrix [ i + 1 ] [ j + 1 ] = 1 ; else mappedMatrix [ i + 1 ] [ j + 1 ] = 0 ; } } } int main ( ) { int n = 3 ; int m = 3 ; int a [ MAX ] [ MAX ] = { { 2 , 3 , 7 } , { 5 , 4 , 2 } , { 3 , 7 , 11 } } ; int mappedMatrix [ MAX ] [ MAX ] = { 0 } ; preprocessMatrix ( mappedMatrix , a , n...