ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [BOJ] #5373 _ 큐빙
    Problem Solving/BOJ 2019. 10. 20. 22:20
    728x90

    [큐빙] https://www.acmicpc.net/problem/5373

     

    5373번: 큐빙

    문제 루빅스 큐브는 삼차원 퍼즐이다. 보통 루빅스 큐브는 3×3×3개의 작은 정육면체로 이루어져 있다. 퍼즐을 풀려면 각 면에 있는 아홉 개의 작은 정육면체의 색이 동일해야 한다. 큐브는 각 면을 양방향으로 90도 만큼 돌릴 수 있도록 만들어져 있다. 회전이 마친 이후에는, 다른 면을 돌릴 수 있다. 이렇게 큐브의 서로 다른 면을 돌리다 보면, 색을 섞을 수 있다. 이 문제에서는 루빅스 큐브가 모두 풀린 상태에서 시작한다. 윗 면은 흰색, 아랫 면은 노란

    www.acmicpc.net

    엄청나게 귀찮은 시뮬레이션 문제이다.

    큐브가 돌아가는 방식대로 회전시킨 후, 최종적으로 제일 윗면의 색을 출력하는 문제이다.

    큐브가 돌아갈 때 위치를 파악하여 하드코딩하였다.

    사소한 실수를 많이해서 시간이 꽤 오래 걸렸다.;;

     

    [ 소스 코드 ]

    #include <cstdio>
    #include <algorithm>
    #define U 0
    #define B 1
    #define L 2
    #define R 3
    #define F 4
    #define D 5
    
    using namespace std;
    
    char cube[6][3][3];
    char tmp_1, tmp_2, tmp_3;
    
    void init();
    void initTmp(int face, int y_1, int x_1, int y_2, int x_2, int y_3, int x_3);
    void turnCubeFace(int face, char dir);
    void solve(char face, char dir);
    void printUpSide();
    int main() {
    	int T, n;
    	char face, dir, tmp;
    
    	scanf("%d", &T);
    	for (int i = 0; i < T; i++) {
    		scanf("%d%c", &n, &tmp);
    		
    		init();
    		for (int j = 0; j < n; j++) {
    			scanf("%c%c%c", &face, &dir, &tmp);
    			solve(face, dir);
    		}
    		printUpSide();
    	}
    
    	return 0;
    }
    
    void init() {
    	for (int i = 0; i < 3; i++) {
    		for (int j = 0; j < 3; j++) {
    			cube[U][i][j] = 'w';
    			cube[D][i][j] = 'y';
    			cube[F][i][j] = 'r';
    			cube[B][i][j] = 'o';
    			cube[L][i][j] = 'g';
    			cube[R][i][j] = 'b';
    		}
    	}
    }
    
    void initTmp(int face ,int y_1, int x_1, int y_2, int x_2, int y_3, int x_3) {
    	tmp_1 = cube[face][y_1][x_1];
    	tmp_2 = cube[face][y_2][x_2];
    	tmp_3 = cube[face][y_3][x_3];
    }
    
    void turnCubeFace(int face, char dir) {
    	char tmp1 = cube[face][0][0], tmp2 = cube[face][0][1];
    
    	if (dir == '-') {
    		cube[face][0][0] = cube[face][0][2];
    		cube[face][0][1] = cube[face][1][2];
    		cube[face][0][2] = cube[face][2][2];
    		cube[face][1][2] = cube[face][2][1];
    		cube[face][2][2] = cube[face][2][0];
    		cube[face][2][1] = cube[face][1][0];
    		cube[face][2][0] = tmp1;
    		cube[face][1][0] = tmp2;
    	}
    	else {
    		cube[face][0][0] = cube[face][2][0];
    		cube[face][0][1] = cube[face][1][0];
    		cube[face][2][0] = cube[face][2][2];
    		cube[face][1][0] = cube[face][2][1];
    		cube[face][2][2] = cube[face][0][2];
    		cube[face][2][1] = cube[face][1][2];
    		cube[face][0][2] = tmp1;
    		cube[face][1][2] = tmp2;
    	}
    }
    
    void solve(char face, char dir) {
    	int r_l;
    	switch (face) {
    	case 'U':
    		turnCubeFace(U, dir);
    		initTmp(B, 0, 2, 0, 1, 0, 0);
    		if (dir == '+') r_l = L;
    		else r_l = R;
    
    		cube[B][0][2] = cube[r_l][0][2];
    		cube[B][0][1] = cube[r_l][0][1];
    		cube[B][0][0] = cube[r_l][0][0];
    		cube[r_l][0][2] = cube[F][0][2];
    		cube[r_l][0][1] = cube[F][0][1];
    		cube[r_l][0][0] = cube[F][0][0];
    		if (r_l == L) r_l = R;
    		else r_l = L;
    		cube[F][0][2] = cube[r_l][0][2];
    		cube[F][0][1] = cube[r_l][0][1];
    		cube[F][0][0] = cube[r_l][0][0];
    		cube[r_l][0][2] = tmp_1;
    		cube[r_l][0][1] = tmp_2;
    		cube[r_l][0][0] = tmp_3;
    		break;
    
    	case 'D':
    		turnCubeFace(D, dir);
    		initTmp(B, 2, 2, 2, 1, 2, 0);
    		if (dir == '+') r_l = R;
    		else r_l = L;
    
    		cube[B][2][2] = cube[r_l][2][2];
    		cube[B][2][1] = cube[r_l][2][1];
    		cube[B][2][0] = cube[r_l][2][0];
    		cube[r_l][2][2] = cube[F][2][2];
    		cube[r_l][2][1] = cube[F][2][1];
    		cube[r_l][2][0] = cube[F][2][0];
    		if (r_l == L) r_l = R;
    		else r_l = L;
    		cube[F][2][2] = cube[r_l][2][2];
    		cube[F][2][1] = cube[r_l][2][1];
    		cube[F][2][0] = cube[r_l][2][0];
    		cube[r_l][2][2] = tmp_1;
    		cube[r_l][2][1] = tmp_2;
    		cube[r_l][2][0] = tmp_3;
    		break;
    
    	case 'F':
    		turnCubeFace(F, dir);
    		initTmp(U, 2, 0, 2, 1, 2, 2);
    		if (dir == '+') {
    			cube[U][2][0] = cube[L][2][2];
    			cube[U][2][1] = cube[L][1][2];
    			cube[U][2][2] = cube[L][0][2];
    			cube[L][2][2] = cube[D][2][0];
    			cube[L][1][2] = cube[D][2][1];
    			cube[L][0][2] = cube[D][2][2];
    			cube[D][2][0] = cube[R][0][0];
    			cube[D][2][1] = cube[R][1][0];
    			cube[D][2][2] = cube[R][2][0];
    			cube[R][0][0] = tmp_1;
    			cube[R][1][0] = tmp_2;
    			cube[R][2][0] = tmp_3;
    		}
    		else {
    			cube[U][2][0] = cube[R][0][0];
    			cube[U][2][1] = cube[R][1][0];
    			cube[U][2][2] = cube[R][2][0];
    			cube[R][0][0] = cube[D][2][0];
    			cube[R][1][0] = cube[D][2][1];
    			cube[R][2][0] = cube[D][2][2];
    			cube[D][2][0] = cube[L][2][2];
    			cube[D][2][1] = cube[L][1][2];
    			cube[D][2][2] = cube[L][0][2];
    			cube[L][2][2] = tmp_1;
    			cube[L][1][2] = tmp_2;
    			cube[L][0][2] = tmp_3;
    		}
    		break;
    
    	case 'B':
    		turnCubeFace(B, dir);
    		initTmp(U, 0, 0, 0, 1, 0, 2);
    		if (dir == '+') {
    			cube[U][0][0] = cube[R][0][2];
    			cube[U][0][1] = cube[R][1][2];
    			cube[U][0][2] = cube[R][2][2];
    			cube[R][0][2] = cube[D][0][0];
    			cube[R][1][2] = cube[D][0][1];
    			cube[R][2][2] = cube[D][0][2];
    			cube[D][0][0] = cube[L][2][0];
    			cube[D][0][1] = cube[L][1][0];
    			cube[D][0][2] = cube[L][0][0];
    			cube[L][2][0] = tmp_1;
    			cube[L][1][0] = tmp_2;
    			cube[L][0][0] = tmp_3;
    		}
    		else {
    			cube[U][0][0] = cube[L][2][0];
    			cube[U][0][1] = cube[L][1][0];
    			cube[U][0][2] = cube[L][0][0];
    			cube[L][2][0] = cube[D][0][0];
    			cube[L][1][0] = cube[D][0][1];
    			cube[L][0][0] = cube[D][0][2];
    			cube[D][0][0] = cube[R][0][2];
    			cube[D][0][1] = cube[R][1][2];
    			cube[D][0][2] = cube[R][2][2];
    			cube[R][0][2] = tmp_1;
    			cube[R][1][2] = tmp_2;
    			cube[R][2][2] = tmp_3;
    		}
    		break;
    
    	case 'L':
    		turnCubeFace(L, dir);
    		initTmp(U, 0, 0, 1, 0, 2, 0);
    		if (dir == '+') {
    			cube[U][0][0] = cube[B][2][2];
    			cube[U][1][0] = cube[B][1][2];
    			cube[U][2][0] = cube[B][0][2];
    			cube[B][2][2] = cube[D][2][2];
    			cube[B][1][2] = cube[D][1][2];
    			cube[B][0][2] = cube[D][0][2];
    			cube[D][2][2] = cube[F][0][0];
    			cube[D][1][2] = cube[F][1][0];
    			cube[D][0][2] = cube[F][2][0];
    			cube[F][0][0] = tmp_1;
    			cube[F][1][0] = tmp_2;
    			cube[F][2][0] = tmp_3;
    		}
    		else {
    			cube[U][0][0] = cube[F][0][0];
    			cube[U][1][0] = cube[F][1][0];
    			cube[U][2][0] = cube[F][2][0];
    			cube[F][0][0] = cube[D][2][2];
    			cube[F][1][0] = cube[D][1][2];
    			cube[F][2][0] = cube[D][0][2];
    			cube[D][2][2] = cube[B][2][2];
    			cube[D][1][2] = cube[B][1][2];
    			cube[D][0][2] = cube[B][0][2];
    			cube[B][2][2] = tmp_1;
    			cube[B][1][2] = tmp_2;
    			cube[B][0][2] = tmp_3;
    		}
    		break;
    
    	case 'R':
    		turnCubeFace(R, dir);
    		initTmp(U, 0, 2, 1, 2, 2, 2);
    		if (dir == '+') {
    			cube[U][0][2] = cube[F][0][2];
    			cube[U][1][2] = cube[F][1][2];
    			cube[U][2][2] = cube[F][2][2];
    			cube[F][0][2] = cube[D][2][0];
    			cube[F][1][2] = cube[D][1][0];
    			cube[F][2][2] = cube[D][0][0];
    			cube[D][2][0] = cube[B][2][0];
    			cube[D][1][0] = cube[B][1][0];
    			cube[D][0][0] = cube[B][0][0];
    			cube[B][2][0] = tmp_1;
    			cube[B][1][0] = tmp_2;
    			cube[B][0][0] = tmp_3;
    		}
    		else {
    			cube[U][0][2] = cube[B][2][0];
    			cube[U][1][2] = cube[B][1][0];
    			cube[U][2][2] = cube[B][0][0];
    			cube[B][2][0] = cube[D][2][0];
    			cube[B][1][0] = cube[D][1][0];
    			cube[B][0][0] = cube[D][0][0];
    			cube[D][2][0] = cube[F][0][2];
    			cube[D][1][0] = cube[F][1][2];
    			cube[D][0][0] = cube[F][2][2];
    			cube[F][0][2] = tmp_1;
    			cube[F][1][2] = tmp_2;
    			cube[F][2][2] = tmp_3;
    		}
    		break;
    	}
    }
    
    void printUpSide() {
    	for (int i = 0; i < 3; i++) {
    		for (int j = 0; j < 3; j++) {
    			printf("%c", cube[U][i][j]);
    		}
    		printf("\n");
    	}
    }
    반응형

    'Problem Solving > BOJ' 카테고리의 다른 글

    [BOJ] #13458 _ 시험 감독  (0) 2019.10.20
    [BOJ] #12100 _ 2048(Easy)  (0) 2019.10.20
    [BOJ] #5373 _ 큐빙  (0) 2019.10.20
    [BOJ] #1976 _ 여행 가자  (0) 2019.10.20
    [BOJ] #17070 _ 파이프 옮기기1  (0) 2019.08.28
    [BOJ] #14891 _ 톱니바퀴  (0) 2019.08.28

    댓글 0

@Jo Grini's Blog