๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/8911)
< ๊ฑฐ๋ถ์ด >
๋ฌธ์ ํ์ด
๊ฑฐ๋ถ์ด๊ฐ ์ด๋ํ๋ ์์น๋ฅผ ์ขํ๋ก ์ ์ฅํด์ ์ต๋ x, y ๊ฐ์ ๊ตฌํด ์ง์ฌ๊ฐํ์ ๋์ด๋ฅผ ๊ตฌํ๋ค.
my solution (Java)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class _8911_ { // ๊ฑฐ๋ถ์ด
static class Position {
private int x;
private int y;
private int dir;
public Position(int x, int y, int dir) {
this.x = x;
this.y = y;
this.dir = dir; // 0 : ์, 1: ์ฐ, 2: ํ, 3: ์ข
}
}
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(bf.readLine());
for (int i = 0; i < t; i++) {
String str = bf.readLine();
Position position = new Position(0, 0, 0);
int maxX = 0, maxY = 0, minX = 0, minY = 0;
for (int j = 0; j < str.length(); j++) {
if (str.charAt(j) == 'F') {
if (position.dir == 0) {
position.x += 1;
} else if (position.dir == 1) {
position.y += 1;
} else if (position.dir == 2) {
position.x -= 1;
} else {
position.y -= 1;
}
} else if (str.charAt(j) == 'B') {
if (position.dir == 0) {
position.x -= 1;
} else if (position.dir == 1) {
position.y -= 1;
} else if (position.dir == 2) {
position.x += 1;
} else {
position.y += 1;
}
} else if (str.charAt(j) == 'L') {
position.dir += 1;
if (position.dir == 4) {
position.dir = 0;
}
} else if (str.charAt(j) == 'R') {
position.dir -= 1;
if (position.dir == -1) {
position.dir = 3;
}
}
if (position.x > maxX) {
maxX = position.x;
}
if (position.y > maxY) {
maxY = position.y;
}
if (position.x < minX) {
minX = position.x;
}
if (position.y < minY) {
minY = position.y;
}
}
bw.write((maxX + Math.abs(minX)) * (maxY + Math.abs(minY)) + "\n");
}
bw.flush();
}
}
๋ณ์)
t : ํ ์คํธ ์ผ์ด์ค ์
str : ์ปจํธ๋กค ํ๋ก๊ทธ๋จ
position : ๊ฑฐ๋ถ์ด ์์น, ๋ฐฉํฅ
maxX, maxY, minX, minY : ์ต๋ ์ต์ ์ขํ
์ฒ์ ๊ฑฐ๋ถ์ด ์์น๋ฅผ (0.0), ๋ฐฉํฅ์ ์๋ก ์ด๊ธฐํํ๋ค. ์ปจํธ๋กค ํ๋ก๊ทธ๋จ์ ๋ณด๋ฉด์ ๋ค์๊ณผ ๊ฐ์ ์์ ์ ํ๋ค.
1) F ๋ผ๋ฉด
ํ์ฌ ์๋ฅผ ๋ณด๊ณ ์๋ค๋ฉด x+1, ์ค๋ฅธ์ชฝ์ ๋ณด๊ณ ์๋ค๋ฉด y+1, ์๋๋ฅผ ๋ณด๊ณ ์๋ค๋ฉด x-1, ์ผ์ชฝ์ ๋ณด๊ณ ์๋ค๋ฉด y-1
2) B ๋ผ๋ฉด
ํ์ฌ ์๋ฅผ ๋ณด๊ณ ์๋ค๋ฉด x-1, ์ค๋ฅธ์ชฝ์ ๋ณด๊ณ ์๋ค๋ฉด y-1, ์๋๋ฅผ ๋ณด๊ณ ์๋ค๋ฉด x+1, ์ผ์ชฝ์ ๋ณด๊ณ ์๋ค๋ฉด y+1
3) L์ด๋ผ๋ฉด ํ์ฌ ๋ฐฉํฅ +1
4) R์ด๋ผ๋ฉด ํ์ฌ ๋ฐฉํฅ -1
5) ์ด๋ํ x์ขํ์ y์ขํ๋ฅผ ํ์ธํ๋ฉฐ ์ต๋ ์ต์ ์ขํ๋ฅผ ๊ตฌํ๋ค.
์ต์ข ๊ตฌํ ์ต๋ ์ต์ ์ขํ๋ฅผ ์ด์ฉํ์ฌ ์ง์ฌ๊ฐํ์ ๋์ด๋ฅผ ๊ตฌํ๋ค.

'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 1706_ํฌ๋ก์ค์๋ (0) | 2024.02.20 |
---|---|
[Baekjoon] 11437_LCA (0) | 2024.02.19 |
[Baekjoon] 1918_ํ์ ํ๊ธฐ์ (0) | 2024.02.15 |
[Baekjoon] 6986_์ ์ฌํ๊ท (1) | 2024.02.14 |
[Baekjoon] 2312_์ ๋ณต์ํ๊ธฐ (2) | 2024.02.13 |