기본 구현 문제이다.
근데 나는 헤맷다....아직 갈길이 먼것 같다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class 백준_거북이 {
// 상, 우, 하, 좌 (상부터 시계 방향으로)
private static int[] dx = {0, 1, 0, -1};
private static int[] dy = {1, 0, -1, 0};
private static int[][]map = new int[500][500];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
for(int i =0; i<t; i++){
String command = br.readLine();
//여기에 이제 로직 설계
int answer = 0;
answer = caculate(command);
//정답출력
System.out.println(answer);
}
}
private static int caculate(String command) {
int x=0, y = 0; //초기 좌표
int direction = 0; //초기 바라보는 상태 : 상
int maxX =0, minX=0, maxY =0, minY =0;
for(int i =0 ;i<command.length(); i++){
char tempC = command.charAt(i);
if(tempC == 'F'){ //바라보고 있는 방향으로 한칸전진
x += dx[direction];
y += dy[direction];
}else if(tempC == 'B'){
x -= dx[direction];
y -= dy[direction];
}else if(tempC == 'L'){ //방향만 바꿈
direction = (direction+3) %4;
}else if(tempC == 'R'){
direction = (direction+1) %4;
}
maxX = Math.max(x, maxX);
minX = Math.min(x, minX);
maxY = Math.max(y, maxY);
minY = Math.min(y, minY);
}
int weight = maxX- minX;
int height = maxY- minY;
return weight * height;
}
}
방식 중에 유의해야하는 것은 map 배열이 필요없다는 것이고 그냥초기 값을 0,0 으로 가져가면 된다는 것이다.
그리고 최대값 최소값을 x좌표, y좌표 둘다 계산해서 가장 오른쪽으로 갔을때, 가장 왼쪽으로 갔을때, 가장 위로갔을때, 가장 아래로 갔을경우 총 4가지의 경우를 구해서 거기서 가로 세로를 양끝단을 빼주어서 구해준다.
그리고 가로세로를 곱해주면 그게 넓이이다.
'알고리즘' 카테고리의 다른 글
백준 - 수찾기(JAVA) (1) | 2024.10.12 |
---|---|
백준 - 랜선자르기 (JAVA) (0) | 2024.10.12 |
프로그래머스 N개의 최소공배수 (JAVA) (1) | 2024.10.09 |
프로그래머스 - 모음사전(java) (0) | 2024.10.09 |
프로그래머스 _ 소수찾기 (JAVA) (0) | 2024.09.03 |