https://www.acmicpc.net/problem/8911
#include <iostream>
#include <string>
using namespace std;
string cmd;
int left_i, left_j, right_i, right_j;
int cur_i, cur_j;
int cur_dir; // 0:북 1:남 2:동 3:서
int main(void) {
int testcase;
cin >> testcase;
for (int i = 0; i < testcase; i++) {
cin >> cmd;
left_i = left_j = right_i = right_j = 0;
cur_i = 0;
cur_j = 0;
cur_dir = 0;
for (int j = 0; j < cmd.size(); j++) {
if (cmd[j] == 'F') {
if (cur_dir == 0) {
cur_i -= 1;
if (cur_i < left_i) left_i = cur_i;
}
else if (cur_dir == 1) {
cur_i += 1;
if (cur_i > right_i) right_i = cur_i;
}
else if (cur_dir == 2) {
cur_j += 1;
if (cur_j > right_j) right_j = cur_j;
}
else if (cur_dir == 3) {
cur_j -= 1;
if (cur_j < left_j) left_j = cur_j;
}
}
else if (cmd[j] == 'B') {
if (cur_dir == 0) {
cur_i += 1;
if (cur_i > right_i) right_i = cur_i;
}
else if (cur_dir == 1) {
cur_i -= 1;
if (cur_i < left_i) left_i = cur_i;
}
else if (cur_dir == 2) {
cur_j -= 1;
if (cur_j < left_j) left_j = cur_j;
}
else if (cur_dir == 3) {
cur_j += 1;
if (cur_j > right_j) right_j = cur_j;
}
}
else if (cmd[j] == 'L') {
if (cur_dir == 0) {
cur_dir = 3;
}
else if (cur_dir == 1) {
cur_dir = 2;
}
else if (cur_dir == 2) {
cur_dir = 0;
}
else if (cur_dir == 3) {
cur_dir = 1;
}
}
else if (cmd[j] == 'R') {
if (cur_dir == 0) {
cur_dir = 2;
}
else if (cur_dir == 1) {
cur_dir = 3;
}
else if (cur_dir == 2) {
cur_dir = 1;
}
else if (cur_dir == 3) {
cur_dir = 0;
}
}
}
int width = right_i - left_i;
int height = left_j - right_j;
int answer = abs(width * height);
cout << answer << endl;
}
}