ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 - 큐빙 (5373) [C++]
    문제 풀이/백준 2024. 4. 6. 23:08
    반응형

    이 문제는 구현, 시뮬레이션 문제이다.

     

    구현이 매우 까다로운데, 큐브가 매우 작아 구현만 하면 정답이 나온다.

     

    중요한 포인트가 몇 가지 있는데, 첫번째는 면의 기준을 잘 정하는 것이다.

    각 면에 대한 배열을 만들고, 각 면의 상하좌우에는 어떤 면이 존재하는지를 먼저 정하고 구현에 들어가야한다.

     

    두번째로는 각 면을 정면으로 봤을 때 상하좌우 면의 방향을 잘 확인하는 것이다.

    돌렸을 때 역방향인 경우에는 방향을 바꾸어서 집어넣어줘야 제대로 값이 들어가게 된다.

     

    마지막으로 옆면 뿐만 아니라 정면도 같이 돌려줘야 한다. 옆면에 너무 집중하다보니 앞면도 돌려줘야 한다는 사실을 까먹는다. 계속 정답이 안 나오던 문제가 앞면까지 돌려주자마자 바로 정답이 나왔다.

    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
    
    #define UP 0
    #define DOWN 1
    #define FRONT 2
    #define BACK 3
    #define LEFT 4
    #define RIGHT 5
    
    typedef vector<vector<vector<char>>> V;
    
    int n;
    vector<string> commands;
    V cube;
    
    void init() {
    	cube = V(6, vector<vector<char>>(3, vector<char>(3, 0)));
    	commands.clear();
    	for (int y = 0; y < 3; y++) {
    		for (int x = 0; x < 3; x++) {
    			cube[UP][y][x] = 'w';
    		}
    	}
    	for (int y = 0; y < 3; y++) {
    		for (int x = 0; x < 3; x++) {
    			cube[DOWN][y][x] = 'y';
    		}
    	}
    	for (int y = 0; y < 3; y++) {
    		for (int x = 0; x < 3; x++) {
    			cube[FRONT][y][x] = 'r';
    		}
    	}
    	for (int y = 0; y < 3; y++) {
    		for (int x = 0; x < 3; x++) {
    			cube[BACK][y][x] = 'o';
    		}
    	}
    	for (int y = 0; y < 3; y++) {
    		for (int x = 0; x < 3; x++) {
    			cube[LEFT][y][x] = 'g';
    		}
    	}
    	for (int y = 0; y < 3; y++) {
    		for (int x = 0; x < 3; x++) {
    			cube[RIGHT][y][x] = 'b';
    		}
    	}
    }
    
    vector<vector<char>> rollPlane(vector<vector<char>>& temp, char direction) {
    	vector<vector<char>> result = temp;
    	if (direction == '+') {
    		for (int y = 0; y < 3; y++) {
    			for (int x = 0; x < 3; x++) {
    				result[y][x] = temp[2-x][y];
    			}
    		}
    	} else {
    		for (int y = 0; y < 3; y++) {
    			for (int x = 0; x < 3; x++) {
    				result[y][x] = temp[x][2-y];
    			}
    		}
    	}
    	return result;
    }
    
    void up(char direction) {	/// clear
    	V result = cube;
    	vector<vector<char>> plane = rollPlane(cube[UP], direction);
    	result[UP] = plane;
    	if (direction == '+') {	// clock
    		for (int i = 0; i < 3; i++) {
    			result[RIGHT][0][i] = cube[BACK][0][i];
    			result[FRONT][0][i] = cube[RIGHT][0][i];
    			result[LEFT][0][i] = cube[FRONT][0][i];
    			result[BACK][0][i] = cube[LEFT][0][i];
    		}
    	} else {	// counter clock
    		for (int i = 0; i < 3; i++) {
    			result[LEFT][0][i] = cube[BACK][0][i];
    			result[FRONT][0][i] = cube[LEFT][0][i];
    			result[RIGHT][0][i] = cube[FRONT][0][i];
    			result[BACK][0][i] = cube[RIGHT][0][i];
    		}
    	}
    	cube = result;
    }
    
    void down(char direction) {	// clear
    	V result = cube;
    	vector<vector<char>> plane = rollPlane(cube[DOWN], direction);
    	result[DOWN] = plane;
    	if (direction == '+') {	// clock
    		for (int i = 0; i < 3; i++) {
    			result[RIGHT][2][i] = cube[FRONT][2][i];
    			result[BACK][2][i] = cube[RIGHT][2][i];
    			result[LEFT][2][i] = cube[BACK][2][i];
    			result[FRONT][2][i] = cube[LEFT][2][i];
    		}
    	} else {	// counter clock
    		for (int i = 0; i < 3; i++) {
    			result[LEFT][2][i] = cube[FRONT][2][i];
    			result[BACK][2][i] = cube[LEFT][2][i];
    			result[RIGHT][2][i] = cube[BACK][2][i];
    			result[FRONT][2][i] = cube[RIGHT][2][i];
    		}
    	}
    	cube = result;
    }
    
    
    void front(char direction) {	// clear
    	V result = cube;
    	vector<vector<char>> plane = rollPlane(cube[FRONT], direction);
    	result[FRONT] = plane;
    	if (direction == '+') {	// clock
    		for (int i = 0; i < 3; i++) {
    			result[RIGHT][i][0] = cube[UP][2][i];
    			result[DOWN][0][2-i] = cube[RIGHT][i][0];
    			result[LEFT][i][2] = cube[DOWN][0][i];
    			result[UP][2][2-i] = cube[LEFT][i][2];
    		}
    	} else {	// counter clock
    		for (int i = 0; i < 3; i++) {
    			result[LEFT][2-i][2] = cube[UP][2][i];
    			result[DOWN][0][i] = cube[LEFT][i][2];
    			result[RIGHT][2-i][0] = cube[DOWN][0][i];
    			result[UP][2][i] = cube[RIGHT][i][0];
    		}
    	}
    	cube = result;
    }
    
    void back(char direction) {
    	V result = cube;
    	vector<vector<char>> plane = rollPlane(cube[BACK], direction);
    	result[BACK] = plane;
    	if (direction == '+') {	// clock
    		for (int i = 0; i < 3; i++) {
    			result[LEFT][2-i][0] = cube[UP][0][i];
    			result[DOWN][2][i] = cube[LEFT][i][0];
    			result[RIGHT][2-i][2] = cube[DOWN][2][i];
    			result[UP][0][i] = cube[RIGHT][i][2];
    		}
    	} else {	// counter clock
    		for (int i = 0; i < 3; i++) {
    			result[RIGHT][i][2] = cube[UP][0][i];
    			result[DOWN][2][2-i] = cube[RIGHT][i][2];
    			result[LEFT][i][0] = cube[DOWN][2][i];
    			result[UP][0][2-i] = cube[LEFT][i][0];
    		}
    	}
    	cube = result;
    }
    
    void left(char direction) {	// clear
    	V result = cube;
    	vector<vector<char>> plane = rollPlane(cube[LEFT], direction);
    	result[LEFT] = plane;
    	if (direction == '+') {	// clock
    		for (int i = 0; i < 3; i++) {
    			result[FRONT][i][0] = cube[UP][i][0];
    			result[DOWN][i][0] = cube[FRONT][i][0];
    			result[BACK][2-i][2] = cube[DOWN][i][0];
    			result[UP][2-i][0] = cube[BACK][i][2];
    		}
    	} else {	// counter clock
    		for (int i = 0; i < 3; i++) {
    			result[BACK][2-i][2] = cube[UP][i][0];
    			result[DOWN][2-i][0] = cube[BACK][i][2];
    			result[FRONT][i][0] = cube[DOWN][i][0];
    			result[UP][i][0] = cube[FRONT][i][0];
    		}
    	}
    	cube = result;
    }
    
    void right(char direction) {
    	V result = cube;
    	vector<vector<char>> plane = rollPlane(cube[RIGHT], direction);
    	result[RIGHT] = plane;
    	if (direction == '+') {	// clock
    		for (int i = 0; i < 3; i++) {
    			result[BACK][2-i][0] = cube[UP][i][2];
    			result[DOWN][2-i][2] = cube[BACK][i][0];
    			result[FRONT][i][2] = cube[DOWN][i][2];
    			result[UP][i][2] = cube[FRONT][i][2];
    		}
    	} else {	// counter clock
    		for (int i = 0; i < 3; i++) {
    			result[FRONT][i][2] = cube[UP][i][2];
    			result[DOWN][i][2] = cube[FRONT][i][2];
    			result[BACK][2-i][0] = cube[DOWN][i][2];
    			result[UP][2-i][2] = cube[BACK][i][0];
    		}
    	}
    	cube = result;
    }
    
    void input() {
    	init();
    	cin >> n;
    	for (int i = 0; i < n; i++) {
    		string cmd;
    		cin >> cmd;
    		commands.push_back(cmd);
    	}
    }
    
    void solve() {
    	for (string& cmd : commands) {
    		char plane = cmd[0];
    		char direction = cmd[1];
    		switch (plane) {
    		case 'U':
    			up(direction);
    			break;
    		case 'D':
    			down(direction);
    			break;
    		case 'F':
    			front(direction);
    			break;
    		case 'B':
    			back(direction);
    			break;
    		case 'L':
    			left(direction);
    			break;
    		case 'R':
    			right(direction);
    			break;
    		}
    	}
    }
    
    void output() {
    	for (int y = 0; y < 3; y++) {
    		for (int x = 0; x < 3; x++) {
    			cout << cube[UP][y][x];
    		}
    		cout << '\n';
    	}
    }
    
    int main() {
    	ios_base::sync_with_stdio(0);
    	cin.tie(0);
    	cout.tie(0);
    
    	int T;
    	cin >> T;
    	for (int testCase = 0; testCase < T; testCase++) {
    		input();
    		solve();
    		output();
    	}
    	return 0;
    }
    반응형
Designed by Tistory.