Official

B - Go Straight and Turn Right Editorial by en_translator


In order to get accepted for this problem, write a program that actually simulates Takahashi’s move. Specifically, scan the given string \(T\) from the beginning and simulate the move or rotation according to whether each character is S or R. Then, output Takahashi’s final position.

Scanning string \(T\) from beginning can be achieved by the loop feature (like for statement) which is a standard feature in programming language. While scanning \(T\), store Takahashi’s position \((x, y)\) and his direction \(d\) (either east, south, west, or north) in variables, updating them appropriately when advancing or turning.

Here is a sample code in C++.

#include <iostream>
using namespace std;

int main(void)
{
  int n;
  string s;
  cin >> n >> s;
  
  int x = 0, y = 0, d = 0;
  for(int i = 0; i < n; i++){
    if(s[i] == 'S'){
      if(d == 0) x++;
      if(d == 1) y--;
      if(d == 2) x--;
      if(d == 3) y++;
    }
    if(s[i] == 'R') d = (d+1) % 4;
  }
  cout << x << " " << y << endl;
  
  return 0;
}

posted:
last update: