Position of robot after given movements | C ++ implementation to find final position of robot after the complete movement ; Function to find final position of robot after the complete movement ; Traverse the instruction string ' move ' ; For each movement increment its respective counter ; Required final position of robot ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void finalPosition ( string move ) { int l = move . size ( ) ; int countUp = 0 , countDown = 0 ; int countLeft = 0 , countRight = 0 ; for ( int i = 0 ; i < l ; i ++ ) { if ( move [ i ] == ' U ' ) countUp ++ ; else if ( move [ i ] == ' D ' ) countDown ++ ; else if ( move [ i ] == ' L ' ) countLeft ++ ; else if ( move [ i ] == ' R ' ) countRight ++ ; } cout << " Final ▁ Position : ▁ ( " << ( countRight - countLeft ) << " , ▁ " << ( countUp - countDown ) << " ) " << endl ; } int main ( ) { string move = " UDDLLRUUUDUURUDDUULLDRRRR " ; finalPosition ( move ) ; return 0 ; }