Final cell position in the matrix | function to find the final cell position in the given matrix ; to count up , down , left and cright movements ; to store the final coordinate position ; traverse the command array ; calculate final values ; Driver Code
< ? php function finalPos ( $ command , $ n , $ x , $ y ) { $ cup ; $ cdown ; $ cleft ; $ cright ; $ final_x ; $ final_y ; $ cup = $ cdown = $ cleft = $ cright = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ command [ $ i ] == ' U ' ) $ cup ++ ; else if ( $ command [ $ i ] == ' D ' ) $ cdown ++ ; else if ( $ command [ $ i ] == ' L ' ) $ cleft ++ ; else if ( $ command [ $ i ] == ' R ' ) $ cright ++ ; } $ final_x = $ x + ( $ cright - $ cleft ) ; $ final_y = $ y + ( $ cdown - $ cup ) ; echo " Final ▁ Position : ▁ " . " ( " . $ final_x . " , ▁ " . $ final_y . " ) " ; } $ command = " DDLRULL " ; $ n = strlen ( $ command ) ; $ x = 3 ; $ y = 4 ; finalPos ( $ command , $ n , $ x , $ y ) ;