๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 8911_๊ฑฐ๋ถ์ด

๋ฟŒ์•ผ._. 2024. 2. 16. 11:00

Silver III

๋ฌธ์ œ(์ถœ์ฒ˜: 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์ขŒํ‘œ๋ฅผ ํ™•์ธํ•˜๋ฉฐ ์ตœ๋Œ€ ์ตœ์†Œ ์ขŒํ‘œ๋ฅผ ๊ตฌํ•œ๋‹ค.

 

์ตœ์ข… ๊ตฌํ•œ ์ตœ๋Œ€ ์ตœ์†Œ ์ขŒํ‘œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง์‚ฌ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ๊ตฌํ•œ๋‹ค.