본문 바로가기
알고리즘/[ADV] 삼성 SW 역량 테스트 A형

BOJ 5373 : 큐빙 (삼성 SW TEST A형)

by 피로물든딸기 2021. 2. 27.
반응형

삼성 A형 전체 링크

 

www.acmicpc.net/workbook/view/1152 (A형 문제집)

 

www.acmicpc.net/problem/5373

 

이후 설명 및 입/출력은 링크 참고

실제 큐브를 만들어서 직접 돌리면 된다.

시계/반시계 방향에 대해서는 아래처럼 구현하면 된다.

if (clockwise == '+') 
{
	for (int r = 0; r < 3; r++)
		for (int c = 0; c < 3; c++)
			Cube[face][c][3 - 1 - r] = copy[face][r][c];
}
else 
{
	for (int r = 0; r < 3; r++)
		for (int c = 0; c < 3; c++)
			Cube[face][3 - 1 - c][r] = copy[face][r][c];
}

실제 시험장에서 종이에 큐브를 그려가면서 어떤 규칙이 있는지 확인해보는 것이 좋다.

 

그리고 U / D / F / B / L / R의 경우는 반대편의 면을 제외하고 변경하면 된다.

예를 들어 U인 경우 UP과 DOWN을 제외하고 하나씩 변경하면 된다.

물론 미리 원본 copy를 복사해둬야 한다.

case UP:
	if (clockwise == '+') 
	{
		Cube[FRONT][0][0] = copy[RIGHT][0][0];
		Cube[FRONT][0][1] = copy[RIGHT][0][1];
		Cube[FRONT][0][2] = copy[RIGHT][0][2];

		Cube[BACK][0][2] = copy[LEFT][0][2];
		Cube[BACK][0][1] = copy[LEFT][0][1];
		Cube[BACK][0][0] = copy[LEFT][0][0];

		Cube[LEFT][0][0] = copy[FRONT][0][0];
		Cube[LEFT][0][1] = copy[FRONT][0][1];
		Cube[LEFT][0][2] = copy[FRONT][0][2];

		Cube[RIGHT][0][2] = copy[BACK][0][2];
		Cube[RIGHT][0][1] = copy[BACK][0][1];
		Cube[RIGHT][0][0] = copy[BACK][0][0];
	}
	else 
	{
		Cube[FRONT][0][0] = copy[LEFT][0][0];
		Cube[FRONT][0][1] = copy[LEFT][0][1];
		Cube[FRONT][0][2] = copy[LEFT][0][2];

		Cube[BACK][0][2] = copy[RIGHT][0][2];
		Cube[BACK][0][1] = copy[RIGHT][0][1];
		Cube[BACK][0][0] = copy[RIGHT][0][0];

		Cube[LEFT][0][0] = copy[BACK][0][0];
		Cube[LEFT][0][1] = copy[BACK][0][1];
		Cube[LEFT][0][2] = copy[BACK][0][2];

		Cube[RIGHT][0][2] = copy[FRONT][0][2];
		Cube[RIGHT][0][1] = copy[FRONT][0][1];
		Cube[RIGHT][0][0] = copy[FRONT][0][0];
	}
	break;

 

최종 코드는 아래와 같다.

#include <stdio.h>

int n;

#define UP    (0)
#define DOWN  (1)
#define FRONT (2)
#define BACK  (3)
#define LEFT  (4)
#define RIGHT (5)

void init(char cube[6][3][3])
{
	char color[6] = { 'w', 'y', 'r', 'o', 'g', 'b' };

	for (int face = 0; face < 6; face++)
		for (int r = 0; r < 3; r++)
			for (int c = 0; c < 3; c++)
				cube[face][r][c] = color[face];
}

void rotate(char Cube[6][3][3], int face, char clockwise) 
{
	char copy[6][3][3];

	for (int face = 0; face < 6; face++)
		for (int r = 0; r < 3; r++)
			for (int c = 0; c < 3; c++)
				copy[face][r][c] = Cube[face][r][c];

	if (clockwise == '+') 
	{
		for (int r = 0; r < 3; r++)
			for (int c = 0; c < 3; c++)
				Cube[face][c][3 - 1 - r] = copy[face][r][c];
	}
	else 
	{
		for (int r = 0; r < 3; r++)
			for (int c = 0; c < 3; c++)
				Cube[face][3 - 1 - c][r] = copy[face][r][c];
	}

	switch (face)
	{
	case UP:
		if (clockwise == '+') 
		{
			Cube[FRONT][0][0] = copy[RIGHT][0][0];
			Cube[FRONT][0][1] = copy[RIGHT][0][1];
			Cube[FRONT][0][2] = copy[RIGHT][0][2];

			Cube[BACK][0][2] = copy[LEFT][0][2];
			Cube[BACK][0][1] = copy[LEFT][0][1];
			Cube[BACK][0][0] = copy[LEFT][0][0];

			Cube[LEFT][0][0] = copy[FRONT][0][0];
			Cube[LEFT][0][1] = copy[FRONT][0][1];
			Cube[LEFT][0][2] = copy[FRONT][0][2];

			Cube[RIGHT][0][2] = copy[BACK][0][2];
			Cube[RIGHT][0][1] = copy[BACK][0][1];
			Cube[RIGHT][0][0] = copy[BACK][0][0];
		}
		else 
		{
			Cube[FRONT][0][0] = copy[LEFT][0][0];
			Cube[FRONT][0][1] = copy[LEFT][0][1];
			Cube[FRONT][0][2] = copy[LEFT][0][2];

			Cube[BACK][0][2] = copy[RIGHT][0][2];
			Cube[BACK][0][1] = copy[RIGHT][0][1];
			Cube[BACK][0][0] = copy[RIGHT][0][0];

			Cube[LEFT][0][0] = copy[BACK][0][0];
			Cube[LEFT][0][1] = copy[BACK][0][1];
			Cube[LEFT][0][2] = copy[BACK][0][2];

			Cube[RIGHT][0][2] = copy[FRONT][0][2];
			Cube[RIGHT][0][1] = copy[FRONT][0][1];
			Cube[RIGHT][0][0] = copy[FRONT][0][0];
		}
		break;
	case DOWN:
		if (clockwise == '+') 
		{
			Cube[FRONT][2][0] = copy[LEFT][2][0];
			Cube[FRONT][2][1] = copy[LEFT][2][1];
			Cube[FRONT][2][2] = copy[LEFT][2][2];

			Cube[BACK][2][2] = copy[RIGHT][2][2];
			Cube[BACK][2][1] = copy[RIGHT][2][1];
			Cube[BACK][2][0] = copy[RIGHT][2][0];

			Cube[LEFT][2][2] = copy[BACK][2][2];
			Cube[LEFT][2][1] = copy[BACK][2][1];
			Cube[LEFT][2][0] = copy[BACK][2][0];

			Cube[RIGHT][2][0] = copy[FRONT][2][0];
			Cube[RIGHT][2][1] = copy[FRONT][2][1];
			Cube[RIGHT][2][2] = copy[FRONT][2][2];
		}
		else 
		{
			Cube[FRONT][2][0] = copy[RIGHT][2][0];
			Cube[FRONT][2][1] = copy[RIGHT][2][1];
			Cube[FRONT][2][2] = copy[RIGHT][2][2];

			Cube[BACK][2][2] = copy[LEFT][2][2];
			Cube[BACK][2][1] = copy[LEFT][2][1];
			Cube[BACK][2][0] = copy[LEFT][2][0];

			Cube[LEFT][2][2] = copy[FRONT][2][2];
			Cube[LEFT][2][1] = copy[FRONT][2][1];
			Cube[LEFT][2][0] = copy[FRONT][2][0];

			Cube[RIGHT][2][0] = copy[BACK][2][0];
			Cube[RIGHT][2][1] = copy[BACK][2][1];
			Cube[RIGHT][2][2] = copy[BACK][2][2];
		}
		break;
	case FRONT:
		if (clockwise == '+')
		{
			Cube[UP][2][0] = copy[LEFT][2][2];
			Cube[UP][2][1] = copy[LEFT][1][2];
			Cube[UP][2][2] = copy[LEFT][0][2];

			Cube[DOWN][0][0] = copy[RIGHT][2][0];
			Cube[DOWN][0][1] = copy[RIGHT][1][0];
			Cube[DOWN][0][2] = copy[RIGHT][0][0];

			Cube[LEFT][0][2] = copy[DOWN][0][0];
			Cube[LEFT][1][2] = copy[DOWN][0][1];
			Cube[LEFT][2][2] = copy[DOWN][0][2];

			Cube[RIGHT][0][0] = copy[UP][2][0];
			Cube[RIGHT][1][0] = copy[UP][2][1];
			Cube[RIGHT][2][0] = copy[UP][2][2];
		}
		else
		{
			Cube[UP][2][0] = copy[RIGHT][0][0];
			Cube[UP][2][1] = copy[RIGHT][1][0];
			Cube[UP][2][2] = copy[RIGHT][2][0];

			Cube[DOWN][0][0] = copy[LEFT][0][2];
			Cube[DOWN][0][1] = copy[LEFT][1][2];
			Cube[DOWN][0][2] = copy[LEFT][2][2];

			Cube[LEFT][0][2] = copy[UP][2][2];
			Cube[LEFT][1][2] = copy[UP][2][1];
			Cube[LEFT][2][2] = copy[UP][2][0];

			Cube[RIGHT][0][0] = copy[DOWN][0][2];
			Cube[RIGHT][1][0] = copy[DOWN][0][1];
			Cube[RIGHT][2][0] = copy[DOWN][0][0];
		}
		break;
	case BACK:
		if (clockwise == '+')
		{
			Cube[UP][0][2] = copy[RIGHT][2][2];
			Cube[UP][0][1] = copy[RIGHT][1][2];
			Cube[UP][0][0] = copy[RIGHT][0][2];

			Cube[DOWN][2][2] = copy[LEFT][2][0];
			Cube[DOWN][2][1] = copy[LEFT][1][0];
			Cube[DOWN][2][0] = copy[LEFT][0][0];

			Cube[LEFT][0][0] = copy[UP][0][2];
			Cube[LEFT][1][0] = copy[UP][0][1];
			Cube[LEFT][2][0] = copy[UP][0][0];

			Cube[RIGHT][0][2] = copy[DOWN][2][2];
			Cube[RIGHT][1][2] = copy[DOWN][2][1];
			Cube[RIGHT][2][2] = copy[DOWN][2][0];
		}
		else
		{
			Cube[UP][0][2] = copy[LEFT][0][0];
			Cube[UP][0][1] = copy[LEFT][1][0];
			Cube[UP][0][0] = copy[LEFT][2][0];

			Cube[DOWN][2][2] = copy[RIGHT][0][2];
			Cube[DOWN][2][1] = copy[RIGHT][1][2];
			Cube[DOWN][2][0] = copy[RIGHT][2][2];

			Cube[LEFT][0][0] = copy[DOWN][2][0];
			Cube[LEFT][1][0] = copy[DOWN][2][1];
			Cube[LEFT][2][0] = copy[DOWN][2][2];

			Cube[RIGHT][0][2] = copy[UP][0][0];
			Cube[RIGHT][1][2] = copy[UP][0][1];
			Cube[RIGHT][2][2] = copy[UP][0][2];
		}
		break;
	case LEFT:
		if (clockwise == '+')
		{
			Cube[UP][0][0] = copy[BACK][2][2];
			Cube[UP][1][0] = copy[BACK][1][2];
			Cube[UP][2][0] = copy[BACK][0][2];

			Cube[DOWN][2][0] = copy[FRONT][2][0];
			Cube[DOWN][1][0] = copy[FRONT][1][0];
			Cube[DOWN][0][0] = copy[FRONT][0][0];

			Cube[FRONT][0][0] = copy[UP][0][0];
			Cube[FRONT][1][0] = copy[UP][1][0];
			Cube[FRONT][2][0] = copy[UP][2][0];

			Cube[BACK][0][2] = copy[DOWN][2][0];
			Cube[BACK][1][2] = copy[DOWN][1][0];
			Cube[BACK][2][2] = copy[DOWN][0][0];
		}
		else 
		{
			Cube[UP][0][0] = copy[FRONT][0][0];
			Cube[UP][1][0] = copy[FRONT][1][0];
			Cube[UP][2][0] = copy[FRONT][2][0];

			Cube[DOWN][2][0] = copy[BACK][0][2];
			Cube[DOWN][1][0] = copy[BACK][1][2];
			Cube[DOWN][0][0] = copy[BACK][2][2];

			Cube[FRONT][0][0] = copy[DOWN][0][0];
			Cube[FRONT][1][0] = copy[DOWN][1][0];
			Cube[FRONT][2][0] = copy[DOWN][2][0];

			Cube[BACK][0][2] = copy[UP][2][0];
			Cube[BACK][1][2] = copy[UP][1][0];
			Cube[BACK][2][2] = copy[UP][0][0];
		}
		break;
	case RIGHT:
		if (clockwise == '+') 
		{
			Cube[UP][2][2] = copy[FRONT][2][2];
			Cube[UP][1][2] = copy[FRONT][1][2];
			Cube[UP][0][2] = copy[FRONT][0][2];

			Cube[DOWN][0][2] = copy[BACK][2][0];
			Cube[DOWN][1][2] = copy[BACK][1][0];
			Cube[DOWN][2][2] = copy[BACK][0][0];

			Cube[FRONT][0][2] = copy[DOWN][0][2];
			Cube[FRONT][1][2] = copy[DOWN][1][2];
			Cube[FRONT][2][2] = copy[DOWN][2][2];

			Cube[BACK][0][0] = copy[UP][2][2];
			Cube[BACK][1][0] = copy[UP][1][2];
			Cube[BACK][2][0] = copy[UP][0][2];
		}
		else 
		{
			Cube[UP][2][2] = copy[BACK][0][0];
			Cube[UP][1][2] = copy[BACK][1][0];
			Cube[UP][0][2] = copy[BACK][2][0];

			Cube[DOWN][0][2] = copy[FRONT][0][2];
			Cube[DOWN][1][2] = copy[FRONT][1][2];
			Cube[DOWN][2][2] = copy[FRONT][2][2];

			Cube[FRONT][0][2] = copy[UP][0][2];
			Cube[FRONT][1][2] = copy[UP][1][2];
			Cube[FRONT][2][2] = copy[UP][2][2];

			Cube[BACK][0][0] = copy[DOWN][2][2];
			Cube[BACK][1][0] = copy[DOWN][1][2];
			Cube[BACK][2][0] = copy[DOWN][0][2];
		}
		break;
	}
}

void output(char cube[6][3][3])
{
	for (int r = 0; r < 3; r++)
	{
		for (int c = 0; c < 3; c++)
			printf("%c", cube[UP][r][c]);
		putchar('\n');
	}
}

int main(void)
{
	int n;

	scanf("%d", &n);

	char changeFace['Z'] = { 0 };

	changeFace['U'] = 0;
	changeFace['D'] = 1;
	changeFace['F'] = 2;
	changeFace['B'] = 3;
	changeFace['L'] = 4;
	changeFace['R'] = 5;

	for (int i = 0; i < n; i++) 
	{
		int m;
		char face, dir;
		char cube[6][3][3];

		init(cube);

		scanf("%d", &m);

		for (int k = 0; k < m; k++)
		{
			scanf(" %c %c", &face, &dir);
			rotate(cube, changeFace[face], dir);
		}

		output(cube);
	}

	return 0;
}
반응형

댓글