Final cell position in the matrix | Java implementation to find the final cell position in the given 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
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void finalPos ( String command , int n , int x , int y ) { int cup , cdown , cleft , cright ; int final_x , final_y ; cup = cdown = cleft = cright = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( command . charAt ( i ) == ' U ' ) cup ++ ; else if ( command . charAt ( i ) == ' D ' ) cdown ++ ; else if ( command . charAt ( i ) == ' L ' ) cleft ++ ; else if ( command . charAt ( i ) == ' R ' ) cright ++ ; } final_x = x + ( cright - cleft ) ; final_y = y + ( cdown - cup ) ; System . out . println ( " Final ▁ Position : ▁ " + " ( " + final_x + " , ▁ " + final_y + " ) " ) ; } public static void main ( String [ ] args ) { String command = " DDLRULL " ; int n = command . length ( ) ; int x = 3 , y = 4 ; finalPos ( command , n , x , y ) ; } }