Notice
Recent Posts
Recent Comments
Link
거의 알고리즘 일기장
백준 5373번 _ 큐빙 본문
5373번: 큐빙
각 테스트 케이스에 대해서 큐브를 모두 돌린 후의 윗 면의 색상을 출력한다. 첫 번째 줄에는 뒷 면과 접하는 칸의 색을 출력하고, 두 번째, 세 번째 줄은 순서대로 출력하면 된다. 흰색은 w, 노란
www.acmicpc.net
풀이방법
미리 큐브가 회전하는 모든경우의 수를 종이에 그려본다 -> 구현!
전체코드
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//int uOut[12] = { 33, 34, 35, 18, 21, 24, 38, 37, 36, 8, 5, 2 };
//int dOut[12] = { 42, 43, 44, 26, 23, 20, 29, 28, 27, 0, 3, 6 };
//int fOut[12] = { 15, 16, 17, 24, 25, 26, 47, 46, 45, 6, 7, 8 };
//int bOut[12] = { 11, 10, 9, 2, 1, 0, 51, 52, 53, 20, 19, 18 };
//int lOut[12] = { 9, 12, 15, 36, 39, 42, 45, 48, 51, 27, 30, 33 };
//int rOut[12] = { 17, 14, 11, 35, 32, 29, 53, 50, 47, 44, 41, 38 };
//
//int uIn[8] = { 9, 10, 11, 14, 17, 16, 15, 12 };
//int dIn[8] = { 45, 46, 47, 50, 53, 52, 51, 48 };
//int fIn[8] = { 36, 37, 38, 41, 44, 43, 42, 39 };
//int bIn[8] = { 35, 34, 33, 30, 27, 28, 29, 32 };
//int lIn[8] = { 2, 5, 8, 7, 6, 3, 0, 1 };
//int rIn[8] = { 24, 21, 18, 19, 20, 23, 26, 25 };
char cube[54];
//u, d, f, b, l, r 순
int out[6][12] = { { 33, 34, 35, 18, 21, 24, 38, 37, 36, 8, 5, 2 },
{ 42, 43, 44, 26, 23, 20, 29, 28, 27, 0, 3, 6 },
{ 15, 16, 17, 24, 25, 26, 47, 46, 45, 6, 7, 8 },
{ 11, 10, 9, 2, 1, 0, 51, 52, 53, 20, 19, 18 },
{ 9, 12, 15, 36, 39, 42, 45, 48, 51, 27, 30, 33 },
{ 17, 14, 11, 35, 32, 29, 53, 50, 47, 44, 41, 38 }
};
int in[6][8] = { { 9, 10, 11, 14, 17, 16, 15, 12 },
{ 45, 46, 47, 50, 53, 52, 51, 48 },
{ 36, 37, 38, 41, 44, 43, 42, 39 },
{ 35, 34, 33, 30, 27, 28, 29, 32 },
{ 2, 5, 8, 7, 6, 3, 0, 1 },
{ 24, 21, 18, 19, 20, 23, 26, 25 }
};
void cubeInit()
{
for (int i = 0; i < 9; i++)
cube[i] = 'g';
for (int i = 9; i < 18; i++)
cube[i] = 'w';
for (int i = 18; i < 27; i++)
cube[i] = 'b';
for (int i = 27; i < 36; i++)
cube[i] = 'o';
for (int i = 36; i < 45; i++)
cube[i] = 'r';
for (int i = 45; i < 54; i++)
cube[i] = 'y';
}
void printUpface()
{
int temp = 0;
for (int i = 9; i <= 17; i++)
{
cout << cube[i];
temp++;
if (temp == 3)
{
cout << endl;
temp = 0;
}
}
}
//u, d, f, b, l, r 순
void CW(char face)
{
vector<char> outSide;
vector<char> inSide;
int idx;
if (face == 'U')
idx = 0;
else if (face == 'D')
idx = 1;
else if (face == 'F')
idx = 2;
else if (face == 'B')
idx = 3;
else if (face == 'L')
idx = 4;
else if (face == 'R')
idx = 5;
//밖
for (int i = 0; i < 12; i++)
outSide.push_back(cube[out[idx][i]]);
//뒤에 있는 세개를 앞에다 붙여야함..ㅎ
vector<char>outChange;
for (int i = 1; i <= 3; i++)
outChange.push_back(*(outSide.end()-i));
reverse(begin(outChange), end(outChange));
for (int i = 0; i < outSide.size() - 3; i++)
outChange.push_back(outSide[i]);
//안
for (int i = 0; i < 8; i++)
inSide.push_back(cube[in[idx][i]]);
//뒤에 있는 두개를 앞에다 붙여야함..ㅎ
vector<char>inChange;
for (int i = 1; i <= 2; i++)
inChange.push_back(*(inSide.end()-i));
reverse(begin(inChange), end(inChange));
for (int i = 0; i < inSide.size() - 2; i++)
inChange.push_back(inSide[i]);
//바꿔주기.ㅠ
for (int i = 0; i < 12; i++)
cube[out[idx][i]] = outChange[i];
for (int i = 0; i < 8; i++)
cube[in[idx][i]] = inChange[i];
}
void simulation()
{
int numOfChange;
cin >> numOfChange;
for (int i = 0; i < numOfChange; i++)
{
string str;
cin >> str;
char face = str[0];
char cwOfccw = str[1];
if (cwOfccw == '+')
CW(face);
else if(cwOfccw == '-')
for(int j =0; j<3; j++)
CW(face);
}
//윗면 출력
printUpface();
}
int main()
{
int testcase;
cin >> testcase;
for (int i = 0; i < testcase; i++)
{
cubeInit();
simulation();
}
return 0;
}
반응형
'알고리즘 문제풀이' 카테고리의 다른 글
백준 1175번 _ 배달 (2) | 2020.11.07 |
---|---|
백준 1194번 _ 달이 차오른다, 가자. (0) | 2020.11.07 |
백준 17472번 _ 다리 만들기 2 (0) | 2020.10.12 |
백준 5213번 _ 과외맨 (0) | 2020.10.12 |
백준 16988번 _ Baaaaaaaaaduk2 (Easy) (0) | 2020.10.12 |
Comments