슈콩

[BOJ] 5373 큐빙 본문

Algorithms/Baekjoon

[BOJ] 5373 큐빙

shukong 2025. 12. 4. 21:44

 

 

[문제]

https://www.acmicpc.net/problem/5373

 

 

[소스 코드]

import java.io.*;
import java.util.*;

public class Main {
	static Face[] cube;
	static HashMap<Character,Integer> faceIdxMap = new HashMap<>();
	static HashMap<Integer,Character> faceColorMap = new HashMap<>();
	static int[][][] edgeInfo = {
			{{2,0,2},{2,0,1},{2,0,0},{4,2,2},{4,1,2},{4,0,2},{3,2,0},{3,2,1},{3,2,2},{5,0,0},{5,1,0},{5,2,0}},
			{{3,0,2},{3,0,1},{3,0,0},{4,0,0},{4,1,0},{4,2,0},{2,2,0},{2,2,1},{2,2,2},{5,2,2},{5,1,2},{5,0,2}},
			{{1,0,2},{1,0,1},{1,0,0},{4,2,0},{4,2,1},{4,2,2},{0,2,0},{0,2,1},{0,2,2},{5,2,0},{5,2,1},{5,2,2}},
			{{0,0,2},{0,0,1},{0,0,0},{4,0,2},{4,0,1},{4,0,0},{1,2,0},{1,2,1},{1,2,2},{5,0,2},{5,0,1},{5,0,0}},
			{{2,0,0},{2,1,0},{2,2,0},{1,0,0},{1,1,0},{1,2,0},{3,0,0},{3,1,0},{3,2,0},{0,0,0},{0,1,0},{0,2,0}},
			{{2,2,2},{2,1,2},{2,0,2},{0,2,2},{0,1,2},{0,0,2},{3,2,2},{3,1,2},{3,0,2},{1,2,2},{1,1,2},{1,0,2}}
	};
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int n = Integer.parseInt(br.readLine());
		mapping();
		for(int i=0;i<n;i++) {
			cube = new Face[6];
			for(int j=0;j<6;j++) {
				cube[j] = new Face(j,faceColorMap.get(j));
			}
			int k = Integer.parseInt(br.readLine());
			st = new StringTokenizer(br.readLine());
			for(int j=0;j<k;j++) {
				String str = st.nextToken();
				int face = faceIdxMap.get(str.charAt(0));
				char dir = str.charAt(1);
				if(dir=='+') cube[face].clockWise();
				else if(dir=='-') cube[face].counterClockWise();
			}
			for(int r=0;r<3;r++) {
				for(int c=0;c<3;c++) {
					System.out.print(cube[0].color[r][c]);
				}
				System.out.println();
			}
		}
	}
	static class Face{
		int face;
		char[][] color;
		Face(int face,char c){
			this.face = face;
			color = new char[3][3];
			for(int i=0;i<3;i++) {
				for(int j=0;j<3;j++) {
					color[i][j] = c;
				}
			}
		}
		void clockWise() {
			char[][] copy = new char[3][3];
			for(int i=0;i<3;i++) copy[i] = color[i].clone();
			for(int i=0;i<3;i++) {
				for(int j=0;j<3;j++) {
					color[i][j] = copy[2-j][i];
				}
			}
			char[] temp = new char[3];
			for(int i=0;i<3;i++) {
				temp[i] = cube[edgeInfo[face][i+9][0]].color[edgeInfo[face][i+9][1]][edgeInfo[face][i+9][2]];
			}
			for(int i=8;i>=0;i--) {
				cube[edgeInfo[face][i+3][0]].change(edgeInfo[face][i+3][1],edgeInfo[face][i+3][2],
						cube[edgeInfo[face][i][0]].color[edgeInfo[face][i][1]][edgeInfo[face][i][2]]);
			}
			for(int i=0;i<3;i++) {
				cube[edgeInfo[face][i][0]].change(edgeInfo[face][i][1],edgeInfo[face][i][2],temp[i]);
			}
		}
		void counterClockWise() {
			char[][] copy = new char[3][3];
			for(int i=0;i<3;i++) copy[i] = color[i].clone();
			for(int i=0;i<3;i++) {
				for(int j=0;j<3;j++) {
					color[i][j] = copy[j][2-i];
				}
			}
			char[] temp = new char[3];
			for(int i=0;i<3;i++) {
				temp[i] = cube[edgeInfo[face][i][0]].color[edgeInfo[face][i][1]][edgeInfo[face][i][2]];
			}
			for(int i=0;i<=8;i++) {
				cube[edgeInfo[face][i][0]].change(edgeInfo[face][i][1],edgeInfo[face][i][2],
						cube[edgeInfo[face][i+3][0]].color[edgeInfo[face][i+3][1]][edgeInfo[face][i+3][2]]);
			}
			for(int i=0;i<3;i++) {
				cube[edgeInfo[face][i+9][0]].change(edgeInfo[face][i+9][1], edgeInfo[face][i+9][2], temp[i]);
			}
		}
		void change(int row,int col,char c) {
			color[row][col] = c;
		}
	}
	static void mapping() { 
		faceIdxMap.put('U', 0);
		faceIdxMap.put('D', 1);
		faceIdxMap.put('F', 2);
		faceIdxMap.put('B', 3);
		faceIdxMap.put('L', 4);
		faceIdxMap.put('R', 5);
		faceColorMap.put(0, 'w');
		faceColorMap.put(1, 'y');
		faceColorMap.put(2, 'r');
		faceColorMap.put(3, 'o');
		faceColorMap.put(4, 'g');
		faceColorMap.put(5, 'b');
	}
}

 

 

접근)  cube의 정보 저장 : 면과 해당 면의 각 색깔

edgeInfo : 면을 기준으로 회전했을 경우, 영향을 받는 다른 면의 정보 (면 종류, 면의 위치)

해당 되는 면의 시계 방향, 반시계 방향을 전부 확인해서 해당 면의 위치 색깔을 전부 변경 : 면의 위치에서 색깔 변경

 

- 변경하려는 면의 위치 <- 변경이 필요한 다른 면의 위치 색깔

+ 해당 면의 색을 변경하는 change 함수 사용 !

   	static int[][][] edgeInfo = {
			{{3,2,0},{3,2,1},{3,2,2},{5,0,0},{5,1,0},{5,2,0},{2,0,2},{2,0,1},{2,0,0},{4,2,2},{4,1,2},{4,0,2}},
			{{2,2,0},{2,2,1},{2,2,2},{5,2,2},{5,1,2},{5,0,2},{3,0,2},{3,0,1},{3,0,0},{4,2,0},{4,1,0},{4,0,0}},
			{{0,2,0},{0,2,1},{0,2,2},{5,2,0},{5,2,1},{5,2,2},{1,0,2},{1,0,1},{1,0,0},{4,2,0},{4,2,1},{4,2,2}},
			{{1,2,0},{1,2,1},{1,2,2},{5,0,2},{5,0,1},{5,0,0},{0,0,2},{0,0,1},{0,0,0},{4,0,2},{4,0,1},{4,0,0}},
			{{3,0,0},{3,1,0},{3,2,0},{0,0,0},{0,1,0},{0,2,0},{2,0,0},{2,1,0},{2,2,0},{1,0,0},{1,1,0},{1,2,0}},
			{{3,2,2},{3,1,2},{3,0,2},{1,2,2},{1,1,2},{1,0,2},{2,2,2},{2,1,2},{2,0,2},{0,2,2},{0,1,2},{0,0,2}}
	};

 

=> edgeInfo의 시작점을 다르게 하면 왜 안되는지 모르겠다.. 혹시, 알고계신 분이 있으시면 댓글 달아주세요!!

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 16235 나무 재테크  (0) 2025.12.05
[BOJ] 16234 인구 이동  (0) 2025.12.04
[BOJ] 15686 치킨 배달  (0) 2025.12.03
[BOJ] 15685 드래곤 커브  (0) 2025.12.03
[BOJ] 15684 사다리 조작  (0) 2025.12.02