Final cell position in the matrix | C ++ 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 program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; void finalPos ( char 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 [ 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 ) ; cout << " Final ▁ Position : ▁ " << " ( " << final_x << " , ▁ " << final_y << " ) " ; } int main ( ) { char command [ ] = " DDLRULL " ; int n = ( sizeof ( command ) / sizeof ( char ) ) - 1 ; int x = 3 , y = 4 ; finalPos ( command , n , x , y ) ; return 0 ; }