Post

백준_8911 (거북이)

구현방법

  1. 문자열을 입력받은 후 각 문자에 대해 작동할 로직을 지정
  2. 회전하는 경우엔 방향만을 바꾸고 전진 or 후진하는 경우에는 현재 방향을 고려하여 현재 좌표(curX, curY)을 업데이트
  3. 현재 좌표(curX, curY)가 업데이트 되면 min값, max값과 비교하여 최댓값, 최솟값을 업데이트
  4. 루프가 종료되면 최종적인 min, max 값이 초기화 되고, 그 값을 이용하여 최소 면적을 구한다.

Code

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.company.baekjoon._8911;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class 한규호_8911 {

  static final int NORTH = 1;
  static final int SOUTH = 2;
  static final int EAST = 3;
  static final int WEST = 4;

  static final char FORWARD = 'F'; // 전진
  static final char BACKWARD = 'B'; // 후진
  static final char LEFT = 'L'; // 왼쪽 회전
  static final char RIGHT = 'R'; // 오른쪽 회전

  static int minX;
  static int maxX;
  static int minY;
  static int maxY;
  static int curX; // 현재 x 좌표
  static int curY; // 현재 y 좌표

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

    int minArea;
    int t = Integer.parseInt(br.readLine());

    while (t-- > 0) {
      String testCase = br.readLine();
      minArea = getMinimumArea(testCase);
      bw.write(minArea + "\n");
    }
    bw.close();
    br.close();
  }

  public static int getMinimumArea(String testCase) {
    setToZero();
    int direction = NORTH;
    for (char c : testCase.toCharArray()) {
      switch (c) {
        case FORWARD:
        case BACKWARD:
          setCurrentPosition(direction, c);
          setMinMaxXY();
          break;
        case LEFT:
        case RIGHT:
          direction = getDirection(direction, c);
          break;
        default:
          break;
      }
    }
    return (maxX - minX) * (maxY - minY);
  }

  public static void setToZero() {
    minX = 0;
    minY = 0;
    maxX = 0;
    maxY = 0;
    curX = 0;
    curY = 0;
  }

  public static void setMinMaxXY() {
    minX = Math.min(minX, curX);
    maxX = Math.max(maxX, curX);
    minY = Math.min(minY, curY);
    maxY = Math.max(maxY, curY);
  }

  public static void setCurrentPosition(int direction, char moveType) {
    if (moveType == FORWARD) {
      if (direction == NORTH) {
        curY++;
      } else if (direction == SOUTH) {
        curY--;
      } else if (direction == EAST) {
        curX++;
      } else {
        curX--;
      }
    } else {
      if (direction == NORTH) {
        curY--;
      } else if (direction == SOUTH) {
        curY++;
      } else if (direction == EAST) {
        curX--;
      } else {
        curX++;
      }
    }
  }

  public static int getDirection(int direction, char rotationDir) {
    if (rotationDir == LEFT) {
      if (direction == NORTH) {
        return WEST;
      } else if (direction == SOUTH) {
        return EAST;
      } else if (direction == EAST) {
        return NORTH;
      } else {
        return SOUTH;
      }
    } else {
      if (direction == NORTH) {
        return EAST;
      } else if (direction == SOUTH) {
        return WEST;
      } else if (direction == EAST) {
        return SOUTH;
      } else {
        return NORTH;
      }
    }
  }
}

문제 링크

This post is licensed under CC BY 4.0 by the author.