백준 8911 c++

magicdrill·2024년 7월 27일

백준 문제풀이

목록 보기
404/673

백준 8911 c++

너무 함수를 많이 나눠서 그런지 다른 사람들 풀이보다 메모리와 시간소모가 컸다.
시간 소모는 크게 차이나지는 않은데 메모리 소모가 어디서 크게 발생하는지 확인해 봐야 겠다.

어쨌건 방향과 현 위치를 좌표로 구분해 추적하여 수행하도록 했다.

또한 진행 방향을 바꾸는 과정을 식을 수행하는 방식으로 구현했는데 다른 풀이를 보니 {{0, 1},{-1, 0},{0, -1},{1, 0}}으로 초기화 해놓고 인덱스만 바꾸는 방식으로 해결을 했다. 이게 훨씬 좋은 방법이라고 생각한다.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void move_F(pair<int, int> &location, pair<int, int> &direction)
{
	//cout << "move_F\n";
	location.first += direction.first;
	location.second += direction.second;

	return;
}

void move_B(pair<int, int> &location, pair<int, int> &direction)
{
	//cout << "move_B\n";
	location.first -= direction.first;
	location.second -= direction.second;

	return;
}

void rotate_L(pair<int, int> &direction)
{
	//cout << "rotate_L\n";
	if (direction.first == 0 && direction.second == 1)
	{
		direction.first = -1;
		direction.second = 0;
	}
	else if (direction.first == -1 && direction.second == 0)
	{
		direction.first = 0;
		direction.second = -1;
	}
	else if (direction.first == 0 && direction.second == -1)
	{
		direction.first = 1;
		direction.second = 0;
	}
	else // direction.first == 1 && direction.second == 0
	{
		direction.first = 0;
		direction.second = 1;
	}

	return;
}

void rotate_R(pair<int, int> &direction)
{
	//cout << "rotate_R\n";
	if (direction.first == 0 && direction.second == 1)
	{
		direction.first = 1;
		direction.second = 0;
	}
	else if (direction.first == 1 && direction.second == 0)
	{
		direction.first = 0;
		direction.second = -1;
	}
	else if (direction.first == 0 && direction.second == -1)
	{
		direction.first = -1;
		direction.second = 0;
	}
	else // direction.first == -1 && direction.second == 0
	{
		direction.first = 0;
		direction.second = 1;
	}

	return;
}

void update_value(pair<int, int> location, int* left, int* right, int* top, int* bottom)
{
	if (location.first < *left)
	{
		*left = location.first;
	}
	if (location.first > *right)
	{
		*right = location.first;
	}
	if (location.second < *bottom)
	{
		*bottom = location.second;
	}
	if (location.second > *top)
	{
		*top = location.second;
	}

	return;
}

int make_rectangle(int left, int right, int top, int bottom)
{
	int size = 0;

	size = (right - left) * (top - bottom);

	return size;
}

void find_answer(string statement)
{
	int i;
	int left = 0, right = 0, top = 0, bottom = 0;
	pair<int, int> direction = { 0, 1 };
	pair<int, int> location = { 0, 0 };
	int max_size = 0;

	for (i = 0; i < statement.length(); i++)
	{
		switch (statement[i])
		{
		case 'F':
			move_F(location, direction);
			break;
		case 'B':
			move_B(location, direction);
			break;
		case 'L':
			rotate_L(direction);
			break;
		case 'R':
			rotate_R(direction);
			break;
		default:
			break;
		}
		update_value(location, &left, &right, &top, &bottom);
	}
	max_size = make_rectangle(left, right, top, bottom);
	cout << max_size << "\n";

	return;
}

void input_statement(vector<string>& statement, int T)
{
	int i;
	string temp;

	for (i = 0; i < T; i++)
	{
		cin >> temp;
		statement.push_back(temp);
	}

	return;
}

void test_case()
{
	int T;
	int i;
	vector <string> statement;

	cin >> T;
	input_statement(statement, T);
	for (i = 0; i < T; i++)
	{
		find_answer(statement[i]);
	}

	return;
}

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	test_case();

	return 0;
}

0개의 댓글