[BOJ] 백준 #8911. 거북이 (C++)

1 분 소요

🎨 문제

boj-8911

문제 링크: https://www.acmicpc.net/problem/8911

  • 알고리즘 분류: 구현
  • 난이도: Silver Ⅱ



💬 풀이

  1. 좌표 이동을 위한 dx[4], dx[4] 배열
  2. 방향을 나타내는 변수 direction ― 북0 동1 남2 서3


👩‍💻 코드

C++

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
using namespace std;

// 북0 동1 남2 서3
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };

int main() {
	//freopen("input.txt", "rt", stdin);
	int T;
	cin >> T;
	
	string route;
	while (T--) {
		cin >> route;
		int x = 0, y = 0;
		int max_x = 0, max_y = 0, min_x = 0, min_y = 0;
		int direction = 0; // 북0 동1 남2 서3
		for (int i = 0; i < route.length(); i++) {
			if (route[i] == 'F') {
				x += dx[direction];
				y += dy[direction];
			}
			else if (route[i] == 'B') {
				x -= dx[direction];
				y -= dy[direction];
			}
			else if (route[i] == 'L') { // 3->2, 2->1, 1->0, 0->3
				direction = (direction + 3) % 4;
			}
			else if (route[i] == 'R') { // 0->1, 1->2, 2->3, 3->0
				direction = (direction + 1) % 4;
			}
			max_x = max(x, max_x);
			max_y = max(y, max_y);
			min_x = min(x, min_x);
			min_y = min(y, min_y);
		}

		int area = (max_x - min_x) * (max_y - min_y);
		cout << area << endl;
	}

	return 0;
}


댓글남기기