Advertisement
Salman_CUET_18

Robot Instructions

Aug 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main(void)
  4. {
  5.     int t, n;
  6.     while(cin >> t)
  7.     {
  8.         while(t--)
  9.         {
  10.             cin >> n;
  11.             getchar();
  12.  
  13.             char str[n][15];
  14.             int pos = 0;
  15.  
  16.             for(int i = 0; i < n; i++)
  17.             {
  18.                 gets(str[i]);
  19.                 if(strcmp(str[i], "LEFT") == 0)
  20.                     pos--;
  21.                 else if(strcmp(str[i], "RIGHT") == 0)
  22.                     pos++;
  23.                 else
  24.                 {
  25.                     int b = 1, ind = 0;
  26.                     for(int j = strlen(str[i]) - 1; j >= 0 ; j--)
  27.                     {
  28.                         if(str[i][j] >= '0' && str[i][j] <= '9')
  29.                         {
  30.                             ind += (str[i][j] - '0') * b;
  31.                             b *= 10;
  32.                         }
  33.                     }
  34.                     ind--;
  35.                     if(strcmp(str[ind], "LEFT") == 0)
  36.                         pos--;
  37.                     else if(strcmp(str[ind], "RIGHT") == 0)
  38.                         pos++;
  39.                     strcpy(str[i], str[ind]);
  40.                 }
  41.             }
  42.             cout << pos << endl;
  43.         }
  44.     }
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement