백준 5373 큐빙

문제링크

https://www.acmicpc.net/problem/5373

문제

풀이

그냥 무식하게 구현했다.

코드

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
#define U 0
#define D 1
#define F 2
#define B 3
#define L 4
#define R 5

char cube[6][9];
char color[6] = { 'w','y','r','o','g','b' };
int rot_plus[9]{ 0,3,6,7,8,5,2,1 };
int rot_minus[9]{ 0,1,2,5,8,7,6,3 };
int rside[3]{ 2,5,8 }, lside[3]{ 0,3,6 }, dside[3]{ 6,7,8 }, uside[3]{ 0,1,2 };

int upl[4]{ F,R,B,L };

void init() {
	for (int k = 0; k < 6; k++) {
		for (int i = 0; i < 9; i++) {
				cube[k][i] = color[k];
		}
	}
}
//U,D,F,B,L,R


void rotate_face(int face, char dir) {
	if (dir == '+') {
		for (int k = 0; k < 2; k++) {
			for (int i = 0; i < 7; i++) {
				swap(cube[face][rot_plus[i]], cube[face][rot_plus[i + 1]]);
			}
		}
	}
	if (dir == '-') {
		for (int k = 0; k < 2; k++) {
			for (int i = 0; i < 7; i++) {
				swap(cube[face][rot_minus[i]], cube[face][rot_minus[i + 1]]);
			}
		}
	}
}

void rotate(string r) {
	int face = r[0];
	char dir = r[1];
	if (face == 'U') face = U;
	if (face == 'D') face = D;
	if (face == 'F') face = F;
	if (face == 'B') face = B;
	if (face == 'L') face = L;
	if (face == 'R') face = R;

	rotate_face(face, dir);

	if (face == U) {
		if (dir == '+') {
			for (int i = 0; i < 3; i++) swap(cube[F][uside[i]], cube[R][uside[i]]);
			for (int i = 0; i < 3; i++) swap(cube[R][uside[i]], cube[B][uside[i]]);
			for (int i = 0; i < 3; i++) swap(cube[B][uside[i]], cube[L][uside[i]]);
		}
		if (dir == '-') {
			for (int i = 0; i < 3; i++) swap(cube[F][uside[i]], cube[L][uside[i]]);
			for (int i = 0; i < 3; i++) swap(cube[L][uside[i]], cube[B][uside[i]]);
			for (int i = 0; i < 3; i++) swap(cube[B][uside[i]], cube[R][uside[i]]);
		}
	}
	else if (face == D) {
		if (dir == '+') {
			for (int i = 0; i < 3; i++) swap(cube[F][dside[i]], cube[L][dside[i]]);
			for (int i = 0; i < 3; i++) swap(cube[L][dside[i]], cube[B][dside[i]]);
			for (int i = 0; i < 3; i++) swap(cube[B][dside[i]], cube[R][dside[i]]);
		}
		if (dir == '-') {
			for (int i = 0; i < 3; i++) swap(cube[F][dside[i]], cube[R][dside[i]]);
			for (int i = 0; i < 3; i++) swap(cube[R][dside[i]], cube[B][dside[i]]);
			for (int i = 0; i < 3; i++) swap(cube[B][dside[i]], cube[L][dside[i]]);
		}
	}
	else if (face == F) {
		if (dir == '+') {
			for (int i = 0; i < 3; i++) swap(cube[L][rside[i]], cube[D][uside[i]]);
			for (int i = 0; i < 3; i++) swap(cube[D][uside[i]], cube[R][lside[2-i]]);
			for (int i = 0; i < 3; i++) swap(cube[R][lside[i]], cube[U][dside[i]]);
		}
		if (dir == '-') {
			for (int i = 0; i < 3; i++) swap(cube[L][rside[i]], cube[U][dside[2-i]]);
			for (int i = 0; i < 3; i++) swap(cube[U][dside[i]], cube[R][lside[i]]);
			for (int i = 0; i < 3; i++) swap(cube[R][lside[i]], cube[D][uside[2-i]]);
		}
	}
	else if (face == B) {
		if (dir == '+') {
			for (int i = 0; i < 3; i++) swap(cube[L][lside[i]], cube[U][uside[2-i]]);
			for (int i = 0; i < 3; i++) swap(cube[U][uside[i]], cube[R][rside[i]]);
			for (int i = 0; i < 3; i++) swap(cube[R][rside[i]], cube[D][dside[2-i]]);
		}
		if (dir == '-') {
			for (int i = 0; i < 3; i++) swap(cube[L][lside[i]], cube[D][dside[i]]);
			for (int i = 0; i < 3; i++) swap(cube[D][dside[i]], cube[R][rside[2-i]]);
			for (int i = 0; i < 3; i++) swap(cube[R][rside[i]], cube[U][uside[i]]);
		}
	}
	else if (face == L) {
		if (dir == '+') {
			for (int i = 0; i < 3; i++) swap(cube[U][lside[i]], cube[B][rside[2-i]]);
			for (int i = 0; i < 3; i++) swap(cube[B][rside[i]], cube[D][lside[2-i]]);
			for (int i = 0; i < 3; i++) swap(cube[D][lside[i]], cube[F][lside[i]]);
		}
		if (dir == '-') {
			for (int i = 0; i < 3; i++) swap(cube[F][lside[i]], cube[D][lside[i]]);
			for (int i = 0; i < 3; i++) swap(cube[D][lside[i]], cube[B][rside[2-i]]);
			for (int i = 0; i < 3; i++) swap(cube[B][rside[i]], cube[U][lside[2-i]]);
		}
	}
	else if (face == R) {
		if (dir == '+') {
			for (int i = 0; i < 3; i++) swap(cube[F][rside[i]], cube[D][rside[i]]);
			for (int i = 0; i < 3; i++) swap(cube[D][rside[i]], cube[B][lside[2-i]]);
			for (int i = 0; i < 3; i++) swap(cube[B][lside[i]], cube[U][rside[2-i]]);
		}
		if (dir == '-') {
			for (int i = 0; i < 3; i++) swap(cube[F][rside[i]], cube[U][rside[i]]);
			for (int i = 0; i < 3; i++) swap(cube[U][rside[i]], cube[B][lside[2-i]]);
			for (int i = 0; i < 3; i++) swap(cube[B][lside[i]], cube[D][rside[2-i]]);
		}
	}
}

void solve() {
	init();

	int cnt;
	cin >> cnt;
	for (int i = 0; i < cnt; i++) {
		string r;
		cin >> r;
		rotate(r);
	}
	for (int i = 0; i < 9; i++) {
		cout << cube[U][i];
		if ((i+1) % 3 == 0) cout << '\n';
	}
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	int tc;
	cin >> tc;

	for (int i = 0; i < tc; i++) {
		solve();
	}
	
}

후기

이런 문제 나오면 시간안에 풀 수 있을지 모르겠다.

좋은 웹페이지 즐겨찾기