코드굽는 타자기
Baekjoon[5373] - 큐빙 본문
링크
문제설명
- 시뮬레이션
문제풀이
- 빡구현, 그저 빡구현
문제코드
package baekjoon;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main5373 {
public static char[][] U = new char[3][3];
public static char[][] D = new char[3][3];
public static char[][] F = new char[3][3];
public static char[][] B = new char[3][3];
public static char[][] L = new char[3][3];
public static char[][] R = new char[3][3];
public static void rotate(char[] oper) {
boolean clockwise=true;
switch (oper[1]) {
case '+':
clockwise=true;
break;
case '-':
clockwise=false;
break;
}
switch (oper[0]) {
case 'U':
front_rotate(U, clockwise);
front_rotate(U, clockwise);
side_rotate(B, R, F, L,
new int[][] {{0,2},{0,1},{0,0}},
new int[][] {{0,2},{0,1},{0,0}},
new int[][] {{0,2},{0,1},{0,0}},
new int[][] {{0,2},{0,1},{0,0}}, clockwise);
break;
case 'D':
front_rotate(D, clockwise);
front_rotate(D, clockwise);
side_rotate(F, R, B, L,
new int[][] {{2,0},{2,1},{2,2}},
new int[][] {{2,0},{2,1},{2,2}},
new int[][] {{2,0},{2,1},{2,2}},
new int[][] {{2,0},{2,1},{2,2}}, clockwise);
break;
case 'F':
front_rotate(F, clockwise);
front_rotate(F, clockwise);
side_rotate(U, R, D, L,
new int[][] {{2,0},{2,1},{2,2}},
new int[][] {{0,0},{1,0},{2,0}},
new int[][] {{0,2},{0,1},{0,0}},
new int[][] {{2,2},{1,2},{0,2}}, clockwise);
break;
case 'B':
front_rotate(B, clockwise);
front_rotate(B, clockwise);
side_rotate(U, L, D, R,
new int[][] {{0,2},{0,1},{0,0}},
new int[][] {{0,0},{1,0},{2,0}},
new int[][] {{2,0},{2,1},{2,2}},
new int[][] {{2,2},{1,2},{0,2}}, clockwise);
break;
case 'L':
front_rotate(L, clockwise);
front_rotate(L, clockwise);
side_rotate(U, F, D, B,
new int[][] {{0,0},{1,0},{2,0}},
new int[][] {{0,0},{1,0},{2,0}},
new int[][] {{0,0},{1,0},{2,0}},
new int[][] {{2,2},{1,2},{0,2}}, clockwise);
break;
case 'R':
front_rotate(R, clockwise);
front_rotate(R, clockwise);
side_rotate(U, B, D, F,
new int[][] {{2,2},{1,2},{0,2}},
new int[][] {{0,0},{1,0},{2,0}},
new int[][] {{2,2},{1,2},{0,2}},
new int[][] {{2,2},{1,2},{0,2}}, clockwise);
break;
}
}
public static void front_rotate(char[][] target, boolean clockwise) {
char temp;
// 시계방향
if (clockwise) {
temp = target[0][0];
target[0][0] = target[1][0];
target[1][0] = target[2][0];
target[2][0] = target[2][1];
target[2][1] = target[2][2];
target[2][2] = target[1][2];
target[1][2] = target[0][2];
target[0][2] = target[0][1];
target[0][1] = temp;
}
// 반시계방향
else {
temp = target[0][0];
target[0][0] = target[0][1];
target[0][1] = target[0][2];
target[0][2] = target[1][2];
target[1][2] = target[2][2];
target[2][2] = target[2][1];
target[2][1] = target[2][0];
target[2][0] = target[1][0];
target[1][0] = temp;
}
}
public static void side_rotate(char[][] target0, char[][] target1, char[][] target2, char[][] target3,
int[][] idx0, int[][] idx1, int[][] idx2, int[][] idx3,
boolean clockwise) {
char[] temp = new char[3];
// 시계방향
if (clockwise) {
temp[0] = target0[idx0[0][0]][idx0[0][1]];
temp[1] = target0[idx0[1][0]][idx0[1][1]];
temp[2] = target0[idx0[2][0]][idx0[2][1]];
target0[idx0[0][0]][idx0[0][1]] = target3[idx3[0][0]][idx3[0][1]];
target0[idx0[1][0]][idx0[1][1]] = target3[idx3[1][0]][idx3[1][1]];
target0[idx0[2][0]][idx0[2][1]] = target3[idx3[2][0]][idx3[2][1]];
target3[idx3[0][0]][idx3[0][1]] = target2[idx2[0][0]][idx2[0][1]];
target3[idx3[1][0]][idx3[1][1]] = target2[idx2[1][0]][idx2[1][1]];
target3[idx3[2][0]][idx3[2][1]] = target2[idx2[2][0]][idx2[2][1]];
target2[idx2[0][0]][idx2[0][1]] = target1[idx1[0][0]][idx1[0][1]];
target2[idx2[1][0]][idx2[1][1]] = target1[idx1[1][0]][idx1[1][1]];
target2[idx2[2][0]][idx2[2][1]] = target1[idx1[2][0]][idx1[2][1]];
target1[idx1[0][0]][idx1[0][1]] = temp[0];
target1[idx1[1][0]][idx1[1][1]] = temp[1];
target1[idx1[2][0]][idx1[2][1]] = temp[2];
}
// 반시계방향
else {
temp[0] = target0[idx0[0][0]][idx0[0][1]];
temp[1] = target0[idx0[1][0]][idx0[1][1]];
temp[2] = target0[idx0[2][0]][idx0[2][1]];
target0[idx0[0][0]][idx0[0][1]] = target1[idx1[0][0]][idx1[0][1]];
target0[idx0[1][0]][idx0[1][1]] = target1[idx1[1][0]][idx1[1][1]];
target0[idx0[2][0]][idx0[2][1]] = target1[idx1[2][0]][idx1[2][1]];
target1[idx1[0][0]][idx1[0][1]] = target2[idx2[0][0]][idx2[0][1]];
target1[idx1[1][0]][idx1[1][1]] = target2[idx2[1][0]][idx2[1][1]];
target1[idx1[2][0]][idx1[2][1]] = target2[idx2[2][0]][idx2[2][1]];
target2[idx2[0][0]][idx2[0][1]] = target3[idx3[0][0]][idx3[0][1]];
target2[idx2[1][0]][idx2[1][1]] = target3[idx3[1][0]][idx3[1][1]];
target2[idx2[2][0]][idx2[2][1]] = target3[idx3[2][0]][idx3[2][1]];
target3[idx3[0][0]][idx3[0][1]] = temp[0];
target3[idx3[1][0]][idx3[1][1]] = temp[1];
target3[idx3[2][0]][idx3[2][1]] = temp[2];
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
System.setIn(new FileInputStream("res/baekjoon/5373.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int T = Integer.parseInt(br.readLine());
for (int test_case = 0; test_case < T; test_case++) {
// 큐브 초기화
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
U[i][j] = 'w';
D[i][j] = 'y';
F[i][j] = 'r';
B[i][j] = 'o';
L[i][j] = 'g';
R[i][j] = 'b';
}
}
int N = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
char[] oper;
boolean clockwise=true;
//// 디버깅
// U = new char[][] {{'a','b','c'},{'a','b','c'},{'a','b','c'}};
// D = new char[][] {{'b','c','d'},{'b','c','d'},{'b','c','d'}};
// F = new char[][] {{'c','d','e'},{'c','d','e'},{'c','d','e'}};
// B = new char[][] {{'d','e','f'},{'d','e','f'},{'d','e','f'}};
// L = new char[][] {{'e','f','g'},{'e','f','g'},{'e','f','g'}};
// R = new char[][] {{'f','g','h'},{'f','g','h'},{'f','g','h'}};
//
// rotate(new char[] {'U','+'});
// rotate(new char[] {'U','-'});
// rotate(new char[] {'D','+'});
// rotate(new char[] {'D','-'});
// rotate(new char[] {'F','+'});
// rotate(new char[] {'F','-'});
// rotate(new char[] {'B','+'});
// rotate(new char[] {'B','-'});
// rotate(new char[] {'L','+'});
// rotate(new char[] {'L','-'});
// rotate(new char[] {'R','+'});
// rotate(new char[] {'R','-'});
for (int o = 0; o < N; o++) {
oper = st.nextToken().toCharArray();
rotate(oper);
}//end for oper
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(U[i][j]);
}
System.out.println();
}
}
}
}
아쉬운점
- front_rotate 할 때 1칸만 이동하게 구했했어서 2번 돌림.
- F, B 에서 index실수 많이함
- 상하좌우 순서반전이있어서 좀 신경써야함
- 머리에 쥐나겠음
- 시간(2시간)
잘한점
- 일단 꾸역꾸역 디버깅하면서 수정함
- 디버깅용 큐브 면도 나름 규칙성있게 짰음
- 회전하는 메서드 인덱스랑 배열 인자로 받아서 하도록 만듬.
'알고리즘 > Simulation' 카테고리의 다른 글
Baekjoon[15685] - 드래곤 커브 (1) | 2020.08.30 |
---|---|
Baekjoon[19235] - 모노미노도미노 (1) | 2020.08.02 |
Baekjoon[16235] - 나무 재테크 (0) | 2020.03.02 |
Baekjoon[17406] - 배열 돌리기 4 (0) | 2020.02.26 |
Baekjoon[17135] - 캐슬 디펜스 (1) | 2020.02.25 |
Comments