본문 바로가기
백준 알고리즘/시뮬레이션

백준 큐빙 C++

by paysmile 2019. 10. 15.

#include <iostream>

using namespace std;
const int MAX = 3;
char cube[6][MAX][MAX];
int testcase, counts;
char color[6] = { 'w','g','b','r','o','y' };

void clockwise(int i) {
	int copy[MAX][MAX];
	for (int a = 0; a < 3; a++) {
		for (int b = 0; b < 3; b++) {
			copy[a][b] = cube[i][a][b];
		}
	}

	for (int a = 0; a < 3; a++) {
		for (int b = 0; b < 3; b++) {
			cube[i][a][b] = copy[2 - b][a];
		}
	}
}

void counterclockwise(int i) {
	int copy[MAX][MAX];
	for (int a = 0; a < 3; a++) {
		for (int b = 0; b < 3; b++) {
			copy[a][b] = cube[i][a][b];
		}
	}

	for (int a = 0; a < 3; a++) {
		for (int b = 0; b < 3; b++) {
			cube[i][a][b] = copy[b][2 - a];
		}
	}
}

void move(char dir, char value) {
	int temp[3], temp2[3];
	if (dir == 'U') {
		if (value == '+') {
			//4 2 3 1
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[2][0][k];
				cube[2][0][k] = cube[4][0][2-k];
			}
			for (int k = 0; k < 3; k++) {
				temp2[k] = cube[3][0][k];
				cube[3][0][k] = temp[k];
			}
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[1][0][k];
				cube[1][0][k] = temp2[k];
			}
			for (int k = 0; k < 3; k++) {
				cube[4][0][2-k] = temp[k];
			}
			clockwise(0);
		}
		else {
			//4 1 3 2
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[1][0][k];
				cube[1][0][k] = cube[4][0][2-k];
			}
			for (int k = 0; k < 3; k++) {
				temp2[k] = cube[3][0][k];
				cube[3][0][k] = temp[k];
			}
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[2][0][k];
				cube[2][0][k] = temp2[k];
			}
			for (int k = 0; k < 3; k++) {
				cube[4][0][k] = temp[2-k];
			}
			counterclockwise(0);
		}
	}
	else if (dir == 'D') {
		if (value == '-') {
			//4 2 3 1
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[2][2][k];
				cube[2][2][k] = cube[4][2][2-k];
			}
			for (int k = 0; k < 3; k++) {
				temp2[k] = cube[3][2][k];
				cube[3][2][k] = temp[k];
			}
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[1][2][k];
				cube[1][2][k] = temp2[k];
			}
			for (int k = 0; k < 3; k++) {
				cube[4][2][k] = temp[2-k];
			}
			clockwise(5);
		}
		else {
			//4 1 3 2
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[1][2][k];
				cube[1][2][k] = cube[4][2][2-k];
			}
			for (int k = 0; k < 3; k++) {
				temp2[k] = cube[3][2][k];
				cube[3][2][k] = temp[k];
			}
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[2][2][k];
				cube[2][2][k] = temp2[k];
			}
			for (int k = 0; k < 3; k++) {
				cube[4][2][k] = temp[2-k];
			}
			counterclockwise(5);
		}
	}
	else if (dir == 'F') {
		if (value == '+') {
			//1 0 2 5
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[0][2][k];
				cube[0][2][k] = cube[1][k][2];
			}
			for (int k = 0; k < 3; k++) {
				temp2[k] = cube[2][k][0];
				cube[2][k][0] = temp[k];
			}
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[5][2][k];
				cube[5][2][k] = temp2[2-k];
			}
			for (int k = 0; k < 3; k++) {
				cube[1][k][2] = temp[2-k];
			}
			clockwise(3);
		}
		else {
			//1 5 2 0
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[5][2][k];
				cube[5][2][k] = cube[1][k][2];
			}
			for (int k = 0; k < 3; k++) {
				temp2[k] = cube[2][k][0];
				cube[2][k][0] = temp[k];
			}
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[0][2][k];
				cube[0][2][k] = temp2[2-k];
			}
			for (int k = 0; k < 3; k++) {
				cube[1][k][2] = temp[2-k];
			}
			counterclockwise(3);
		}
	}
	else if (dir == 'B') {
		if (value == '+') {
			//0 1 5 2
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[1][k][0];
				cube[1][k][0] = cube[0][0][2-k];
			}
			for (int k = 0; k < 3; k++) {
				temp2[k] = cube[5][0][k];
				cube[5][0][k] = temp[k];
			}
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[2][k][2];
				cube[2][k][2] = temp2[2-k];
			}
			for (int k = 0; k < 3; k++) {
				cube[0][0][k] = temp[k];
			}
			clockwise(4);
		}
		else {
			//0 2 5 1
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[2][k][2];
				cube[2][k][2] = cube[0][0][k];
			}
			for (int k = 0; k < 3; k++) {
				temp2[k] = cube[5][0][k];
				cube[5][0][k] = temp[2-k];
			}
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[1][k][0];
				cube[1][k][0] = temp2[k];
			}
			for (int k = 0; k < 3; k++) {
				cube[0][0][k] = temp[2-k];
			}
			counterclockwise(4);
		}
	}
	else if (dir == 'L') {
		if (value == '+') {
			//0 3 5 4
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[3][k][0];
				cube[3][k][0] = cube[0][k][0];
			}
			for (int k = 0; k < 3; k++) {
				temp2[k] = cube[5][k][0];
				cube[5][k][0] = temp[2-k];
			}
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[4][k][0];
				cube[4][k][0] = temp2[k];
			}
			for (int k = 0; k < 3; k++) {
				cube[0][k][0] = temp[2-k];
			}
			clockwise(1);
		}
		else {
			//0 4 5 3
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[4][k][0];
				cube[4][k][0] = cube[0][2-k][0];
			}
			for (int k = 0; k < 3; k++) {
				temp2[k] = cube[5][k][0];
				cube[5][k][0] = temp[k];
			}
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[3][k][0];
				cube[3][k][0] = temp2[2-k];
			}
			for (int k = 0; k < 3; k++) {
				cube[0][k][0] = temp[k];
			}
			counterclockwise(1);
		}
	}
	else if (dir == 'R') {
		if (value == '+') {
			//0 4 5 3
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[4][k][2];
				cube[4][k][2] = cube[0][2-k][2];
			}
			for (int k = 0; k < 3; k++) {
				temp2[k] = cube[5][k][2];
				cube[5][k][2] = temp[k];
			}
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[3][k][2];
				cube[3][k][2] = temp2[2-k];
			}
			for (int k = 0; k < 3; k++) {
				cube[0][k][2] = temp[k];
			}
			clockwise(2);
		}
		else {
			//0 3 5 4
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[3][k][2];
				cube[3][k][2] = cube[0][k][2];
			}
			for (int k = 0; k < 3; k++) {
				temp2[k] = cube[5][k][2];
				cube[5][k][2] = temp[2-k];
			}
			for (int k = 0; k < 3; k++) {
				temp[k] = cube[4][k][2];
				cube[4][k][2] = temp2[k];
			}
			for (int k = 0; k < 3; k++) {
				cube[0][k][2] = temp[2-k];
			}
			counterclockwise(2);
		}
	}
}

void back_cube() {
	for (int i = 0; i < 6; i++) {
		for (int j = 0; j < 3; j++) {
			for (int k = 0; k < 3; k++) {
				cube[i][j][k] = color[i];
			}
		}
	}
}

int main(void) {
	cin >> testcase;

	for (int i = 0; i < testcase; i++) {
		cin >> counts;
		back_cube();
		for (int j = 0; j < counts; j++) {
			char dir, value;
			cin >> dir >> value;
			move(dir, value);
		}
		for (int a = 0; a < 3; a++) {
			for (int b = 0; b < 3; b++) {
				cout << cube[0][a][b];
			}
			cout << endl;
		}
	}
}

'백준 알고리즘 > 시뮬레이션' 카테고리의 다른 글

백준 낚시왕 C++  (0) 2019.10.17
백준 나무 재테크 C++  (0) 2019.10.16
백준 1152번 C++  (0) 2019.08.30
백준 1551번 C++  (0) 2019.08.30
백준 1022번 C++  (0) 2019.08.30