문제
입력 및 출력
풀이
import java.io.*;
import java.util.*;
class Main {
public static int minX, maxX;
public static int minY, maxY;
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
String[] strArray = new String[T];
for(int i = 0; i < T; i++) {
strArray[i] = br.readLine();
}
for(int i = 0; i < T; i++) {
minX = 0;
maxX = 0;
minY = 0;
maxY = 0;
int[] pos = {0, 0, 4};
for(int j = 0; j < strArray[i].length(); j++) {
switch(strArray[i].charAt(j)) {
case 'F':
if(pos[2] == 1) {
pos[0]--;
if(pos[0] < minX) {
minX = pos[0];
}
}else if(pos[2] == 2) {
pos[1]--;
if(pos[1] < minY) {
minY = pos[1];
}
}else if(pos[2] == 3) {
pos[0]++;
if(pos[0] > maxX) {
maxX = pos[0];
}
}else if(pos[2] == 4) {
pos[1]++;
if(pos[1] > maxY) {
maxY = pos[1];
}
}
break;
case 'B':
if(pos[2] == 1) {
pos[0]++;
if(pos[0] > maxX) {
maxX = pos[0];
}
}else if(pos[2] == 2) {
pos[1]++;
if(pos[1] > maxY) {
maxY = pos[1];
}
}else if(pos[2] == 3) {
pos[0]--;
if(pos[0] < minX) {
minX = pos[0];
}
}else if(pos[2] == 4) {
pos[1]--;
if(pos[1] < minY) {
minY = pos[1];
}
}
break;
case 'L':
if(pos[2]-1 < 1){
pos[2] = 4;
}else {
pos[2]--;
}
break;
case 'R':
if(pos[2]+1 > 4){
pos[2] = 1;
}else {
pos[2]++;
}
break;
}
}
int xLength = maxX - minX;
int yLength = maxY - minY;
sb.append(xLength * yLength).append("\n");
}
System.out.println(sb);
}
}
결과 및 해결방법
[결과]