Posts 5373 Samsung sw test
Post
Cancel

5373 Samsung sw test

5373 Samsung sw test

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <cstring>
#define MAX_ROTATE	1000
#define MAX_TESTCASE 100
#define WHITE 100
#define YELLOW 200
#define RED 300
#define ORANGE 400
#define GREEN 500
#define BLUE 600

using namespace std;

typedef struct Command {
	int location;	//큐브 면
	int way;		//0이 반시계, 1은 시계
} Command;

int cube[6][9];	//0이 위, 1이 뒤, 2가 오, 3은 앞, 4는 왼쪽, 5는 아래
int numOfTestcase;
Command testcase[MAX_TESTCASE][MAX_ROTATE];
int numOfCommand[MAX_TESTCASE];

void implementTestcases();
void implementSingleTC(Command command[MAX_ROTATE], int numCommand);
void implementSingleCommand(int index[4], int cubeIndex[4][3], int way, int location);
void printCube();
void initCube();

int main() {
	cin >> numOfTestcase;
	for (int i = 0; i < numOfTestcase; i++) {
		cin >> numOfCommand[i];
		for (int j = 0; j < numOfCommand[i]; j++) {
			char temp;
			cin >> temp;

			if (temp == 'U') testcase[i][j].location = 0;
			else if (temp == 'D') testcase[i][j].location = 5;
			else if (temp == 'F') testcase[i][j].location = 3;
			else if (temp == 'B') testcase[i][j].location = 1;
			else if (temp == 'L') testcase[i][j].location = 4;
			else if (temp == 'R') testcase[i][j].location = 2;

			cin >> temp;
			if (temp == '+') testcase[i][j].way = 1;
			else if (temp == '-') testcase[i][j].way = 0;
		}
	}
	implementTestcases();
}

void initCube() {
	int colors[6] = { WHITE, ORANGE, BLUE, RED, GREEN, YELLOW };
	for (int i = 0; i < 6; i++)
		for (int j = 0; j < 9; j++)
			cube[i][j] = colors[i];
}

void implementTestcases() {
	for (int i = 0; i < numOfTestcase; i++) {
		initCube();
		implementSingleTC(testcase[i], numOfCommand[i]);
		printCube();
	}
}

void implementSingleTC(Command command[MAX_ROTATE], int numCommand) {
	for (int i = 0; i < numCommand; i++) {
		if (command[i].location == 0) {	//윗
			int index[4] = { 1,2,3,4 };
			int cubeIndex[4][3] = { {0,1,2},{0,1,2},{0,1,2},{0,1,2} };
			implementSingleCommand(index, cubeIndex,command[i].way, 0);
		}
		else if (command[i].location == 1) {	//뒤
			int index[4] = { 0,4,5,2 };
			int cubeIndex[4][3] = { {2,1,0}, {0,3,6}, {2,1,0}, {8,5,2} };
			implementSingleCommand(index, cubeIndex, command[i].way, 1);
		}
		else if (command[i].location == 2) {	//오
			int index[4] = { 0,1,5,3 };
			int cubeIndex[4][3] = { {8,5,2}, {0,3,6}, {0,3,6}, {8,5,2} };
			implementSingleCommand(index, cubeIndex, command[i].way, 2);
		}
		else if (command[i].location == 3) {	//앞
			int index[4] = { 0,2,5,4 };
			int cubeIndex[4][3] = { {6,7,8}, {0,3,6}, {6,7,8}, {8,5,2} };
			implementSingleCommand(index, cubeIndex, command[i].way, 3);
		}
		else if (command[i].location == 4) {	//왼
			int index[4] = { 0,3,5,1 };
			int cubeIndex[4][3] = { {0,3,6}, {0,3,6}, {8,5,2}, {8,5,2} };
			implementSingleCommand(index, cubeIndex, command[i].way, 4);
		}
		else if (command[i].location == 5) {	//아
			int index[4] = { 1,4,3,2 };
			int cubeIndex[4][3] = { {6,7,8}, {6,7,8},{6,7,8},{6,7,8} };
			implementSingleCommand(index, cubeIndex, command[i].way, 5);
		}
	}
}

void implementSingleCommand(int index[4], int cubeIndex[4][3], int way, int location) {
	if (way == 1) {
		int first = index[0];
		int temp[3] = { cube[first][cubeIndex[0][0]], cube[first][cubeIndex[0][1]], cube[first][cubeIndex[0][2]] };
		for (int r = 1; r < 4; r++) {
			int temp2[3] = { cube[index[r]][cubeIndex[r][0]], cube[index[r]][cubeIndex[r][1]], cube[index[r]][cubeIndex[r][2]] };
			cube[index[r]][cubeIndex[r][0]] = temp[0];
			cube[index[r]][cubeIndex[r][1]] = temp[1];
			cube[index[r]][cubeIndex[r][2]] = temp[2];
			memcpy(temp, temp2, sizeof(int) * 3);
		}
		cube[first][cubeIndex[0][0]] = temp[0];
		cube[first][cubeIndex[0][1]] = temp[1];
		cube[first][cubeIndex[0][2]] = temp[2];
	}
	else {
		int last = index[3];
		int temp[3] = { cube[last][cubeIndex[3][0]], cube[last][cubeIndex[3][1]], cube[last][cubeIndex[3][2]] };
		for (int r = 2; r >= 0; r--) {
			int temp2[3] = { cube[index[r]][cubeIndex[r][0]], cube[index[r]][cubeIndex[r][1]], cube[index[r]][cubeIndex[r][2]] };
			cube[index[r]][cubeIndex[r][0]] = temp[0];
			cube[index[r]][cubeIndex[r][1]] = temp[1];
			cube[index[r]][cubeIndex[r][2]] = temp[2];
			memcpy(temp, temp2, sizeof(int) * 3);
		}
		cube[last][cubeIndex[3][0]] = temp[0];
		cube[last][cubeIndex[3][1]] = temp[1];
		cube[last][cubeIndex[3][2]] = temp[2];
	}


	int tempIndex[4][3] = { {0,1,2}, {2,5,8}, {8,7,6}, {6, 3, 0} };
	int tempValue[4][3] = { {cube[location][0], cube[location][1], cube[location][2]}, {cube[location][2], cube[location][5], cube[location][8]},
		{cube[location][8], cube[location][7], cube[location][6] }, {cube[location][6], cube[location][3], cube[location][0]} };
	if (way == 1) {
		for (int i = 1; i < 4; i++) {
			cube[location][tempIndex[i][0]] = tempValue[i - 1][0];
			cube[location][tempIndex[i][1]] = tempValue[i - 1][1];
			cube[location][tempIndex[i][2]] = tempValue[i - 1][2];
		}
		cube[location][0] = tempValue[3][0];
		cube[location][1] = tempValue[3][1];
		cube[location][2] = tempValue[3][2];
	}
	else {
		for (int i = 2; i >= 0; i--) {
			cube[location][tempIndex[i][0]] = tempValue[i + 1][0];
			cube[location][tempIndex[i][1]] = tempValue[i + 1][1];
			cube[location][tempIndex[i][2]] = tempValue[i + 1][2];
		}
		cube[location][6] = tempValue[0][0];
		cube[location][3] = tempValue[0][1];
		cube[location][0] = tempValue[0][2];
	}
}

void printCube() {
	for (int i = 0; i < 9; i++) {
		char temp;
		if (cube[0][i] == WHITE)
			temp = 'w';
		else if (cube[0][i] == ORANGE)
			temp = 'o';
		else if (cube[0][i] == RED)
			temp = 'r';
		else if (cube[0][i] == GREEN)
			temp = 'g';
		else if (cube[0][i] == BLUE)
			temp = 'b';
		else if (cube[0][i] == YELLOW)
			temp = 'y';
		cout << temp;
		if (i % 3 == 2)
			cout << endl;
	}
}
This post is licensed under CC BY 4.0 by the author.

3568 iSharp

9095 1,2,3 더하기

Comments powered by Disqus.