문제
https://www.acmicpc.net/problem/8911
8911번: 거북이
첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있고, 컨트롤 프로그램이 주어진다. 프로그램은 항상 문제의 설명에 나와있는 네가지 명령으로만 이루어져
www.acmicpc.net
문제 풀이
|
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
47
48
49
50
51
52
53
54
55
56
57
58
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main_BOJ_8911_거북이 {
static int[] dx = {-1,0,1,0};
static int[] dy = {0,1,0,-1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
int ans=0;
for (int tc = 1; tc <= T; tc++) {
//x,y좌표값의 최대최소
int minX =0, minY =0, maxX =0, maxY =0;
//거북이의 x,y의 현재위치
int nowX =0, nowY =0;
//거북이의 방향설정 (북:0, 동:1, 남:2, 서:3)
int dir=0;
String str = br.readLine();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if(c=='F') { //한 눈금 앞으로
nowX = nowX+dx[dir];
nowY = nowY+dy[dir];
}else if(c=='B'){//한 눈금 뒤로
nowX = nowX-dx[dir];
nowY = nowY-dy[dir];
}else if(c=='L') { //왼쪽으로 90도 회전
if(dir==0) dir=3;
else dir--;
}else if(c=='R') { //오른쪽으로 90도 회전
if(dir==3) dir=0;
else dir++;
}
minX=Math.min(minX, nowX);
minY=Math.min(minY, nowY);
maxX=Math.max(maxX, nowX);
maxY=Math.max(maxY, nowY);
}
ans =((Math.abs(minX)+Math.abs(maxX))*(Math.abs(minY)+Math.abs(maxY)));
System.out.println(ans);
}
}
}
|
cs |
'Algorithm > BOJ' 카테고리의 다른 글
| [백준 알고리즘] 백준 2231번 - 분해합(JAVA) (0) | 2022.01.16 |
|---|---|
| [백준 알고리즘] 백준 16916번 - 부분 문자열(JAVA) (0) | 2022.01.14 |
| [백준 알고리즘] 백준 2309번 - 일곱난쟁이 (JAVA) (0) | 2021.11.30 |