• 주어진 명령대로 거북이를 움직이면서 좌표의 최대, 최소를 구한다
  • 시작점은 (0, 0)
  • 최대, 최소의 값들의 직사각형 크기를 구한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.io.*;

public class Main {
	static int N;
	static int[] dx = {-1, 0, 1, 0};
	static int[] dy = { 0, 1, 0,-1};

	static int solution(char[] orders) {
		int dir = 0;
		int r = 0, c = 0;
		int maxR, maxC, minR, minC;
		maxR = maxC = minR = minC = 0;
		for (int i = 0; i < orders.length; i++) {
			char order = orders[i];
			if (order == 'L')
				dir = (dir + 3) % 4;
			else if (order == 'R')
				dir = (dir + 1) % 4;
			else if (order == 'F') {
				r += dx[dir];
				c += dy[dir];
				maxR = Math.max(maxR, r);
				minR = Math.min(minR, r);
				maxC = Math.max(maxC, c);
				minC = Math.min(minC, c);
			}
			else {
				int backDir = (dir + 2) % 4;
				r += dx[backDir];
				c += dy[backDir];
				maxR = Math.max(maxR, r);
				minR = Math.min(minR, r);
				maxC = Math.max(maxC, c);
				minC = Math.min(minC, c);
			}
		}
		return (maxR - minR) * (maxC - minC);
	}
	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		N = Integer.parseInt(in.readLine());
		for (int i = 0; i < N; i++) {
			System.out.println(solution(in.readLine().toCharArray()));
		}
	}
}