관리 메뉴

공부공간

BOJ - 5373 ) 큐빙 본문

구현,시뮬

BOJ - 5373 ) 큐빙

SangwonSeo 개발자가될수있을까? 2021. 10. 2. 10:51
728x90

 


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

 

5373번: 큐빙

각 테스트 케이스에 대해서 큐브를 모두 돌린 후의 윗 면의 색상을 출력한다. 첫 번째 줄에는 뒷 면과 접하는 칸의 색을 출력하고, 두 번째, 세 번째 줄은 순서대로 출력하면 된다. 흰색은 w, 노란

www.acmicpc.net


큐브의 시뮬레이션을 위해서 모든 경우의수에 대해서 색과 방향을 고려해 코딩해준다.

 

맨 윗면이 시계방향으로 돌았을 경우, 옆면의 제일 윗면도 같이 돌아감에 유의하여 구현한다.

 


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		StringBuilder sb = new StringBuilder();
		int T = Integer.parseInt(st.nextToken());
		char temp[] = new char[3];
		char temp2[][] = new char[3][3];
		char cube[][][] = new char[6][3][3];
		char fill[] = {'w','y','r','o','g','b'};
		int rolling_time;
		int c=0;
		for(int tc = 0 ; tc < T ; tc++) {
			st = new StringTokenizer(br.readLine());
			rolling_time = Integer.parseInt(st.nextToken());
			st = new StringTokenizer(br.readLine());
			String methods[] = new String[rolling_time];
			for(int index = 0 ; index < rolling_time ; index++) {methods[index] = st.nextToken();}
			// 0은 위 : 흰색 , 1은 아래 노랑색, 2는 앞 빨강색, 3은 뒷 오렌지색, 4는 왼쪽초록색 ,5는 오른쪽 파란색
			for(int color = 0 ; color < 6 ; color++) {
				for(int y = 0 ; y < 3 ; y ++) {
					for(int x=  0 ; x< 3 ; x++) {
						cube[color][y][x] = fill[color];
						// 큐브 초기화
					}
				}
			}
			for(int index = 0 ; index < rolling_time ; index++) {
				if(methods[index].equals("U+")) {
					c=0;
					for(int i = 0 ; i < 3 ; i++) temp2[i][2] = cube[c][0][i];
					for(int i = 0 ; i < 3 ; i++) temp2[2][2-i] = cube[c][i][2];
					for(int i = 0 ; i < 3 ; i++) temp2[2-i][0] = cube[c][2][2-i];
					for(int i = 0 ; i < 3 ; i++) temp2[0][i] = cube[c][2-i][0];
					
					for(int i = 0 ; i < 3 ; i++) temp[i] = cube[2][0][i];
					for(int i = 0 ; i < 3 ; i++) cube[2][0][i] = cube[5][0][i];
					for(int i = 0 ; i < 3 ; i++) cube[5][0][i] = cube[3][0][i];
					for(int i = 0 ; i < 3 ; i++) cube[3][0][i] = cube[4][0][i];
					for(int i = 0 ; i < 3 ; i++) cube[4][0][i] = temp[i];
					
					temp2[1][1] = cube[c][1][1];
					
					for(int y=  0 ; y < 3 ; y ++) {for(int x = 0 ; x< 3 ; x++) {cube[c][y][x] = temp2[y][x];}}
				
				}else if(methods[index].equals("U-")){
					c = 0;
					for(int i = 0 ; i < 3 ; i++) temp[2-i] = cube[2][0][2-i];
					for(int i = 0 ; i < 3 ; i++) cube[2][0][2-i] = cube[4][0][2-i];
					for(int i = 0 ; i < 3 ; i++) cube[4][0][2-i] = cube[3][0][2-i];
					for(int i = 0 ; i < 3 ; i++) cube[3][0][2-i] = cube[5][0][2-i];
					for(int i = 0 ; i < 3 ; i++) cube[5][0][2-i] = temp[2-i];
					//
					for(int i = 0 ; i < 3 ; i++) temp2[i][0] = cube[c][0][2-i];
					for(int i = 0 ; i < 3 ; i++) temp2[2][i] = cube[c][i][0];
					for(int i = 0 ; i < 3 ; i++) temp2[2-i][2] = cube[c][2][i];
					for(int i = 0 ; i < 3 ; i++) temp2[0][2-i] = cube[c][2-i][2];
					temp2[1][1] = cube[c][1][1];
					for(int y=  0 ; y < 3 ; y ++) {for(int x = 0 ; x< 3 ; x++) {cube[c][y][x] = temp2[y][x];}}
				
				}else if(methods[index].equals("D+")){
					
					c = 1;
					for(int i = 0 ; i < 3 ; i++) temp[2-i] = cube[2][2][2-i];
					for(int i = 0 ; i < 3 ; i++) cube[2][2][2-i] = cube[4][2][2-i];
					for(int i = 0 ; i < 3 ; i++) cube[4][2][2-i] = cube[3][2][2-i];
					for(int i = 0 ; i < 3 ; i++) cube[3][2][2-i] = cube[5][2][2-i];
					for(int i = 0 ; i < 3 ; i++) cube[5][2][2-i] = temp[2-i];
					//
					for(int i = 0 ; i < 3 ; i++) temp2[i][2] = cube[c][0][i];
					for(int i = 0 ; i < 3 ; i++) temp2[2][2-i] = cube[c][i][2];
					for(int i = 0 ; i < 3 ; i++) temp2[2-i][0] = cube[c][2][2-i];
					for(int i = 0 ; i < 3 ; i++) temp2[0][i] = cube[c][2-i][0];
					temp2[1][1] = cube[c][1][1];
					for(int y=  0 ; y < 3 ; y ++) {for(int x = 0 ; x< 3 ; x++) {cube[c][y][x] = temp2[y][x];}}
				
					
				}else if(methods[index].equals("D-")){
					c=1;
					for(int i = 0 ; i < 3 ; i++) temp[i] = cube[2][2][i];
					for(int i = 0 ; i < 3 ; i++) cube[2][2][i] = cube[5][2][i];
					for(int i = 0 ; i < 3 ; i++) cube[5][2][i] = cube[3][2][i];
					for(int i = 0 ; i < 3 ; i++) cube[3][2][i] = cube[4][2][i];
					for(int i = 0 ; i < 3 ; i++) cube[4][2][i] = temp[i];
					//
					for(int i = 0 ; i < 3 ; i++) temp2[i][0] = cube[c][0][2-i];
					for(int i = 0 ; i < 3 ; i++) temp2[2][i] = cube[c][i][0];
					for(int i = 0 ; i < 3 ; i++) temp2[2-i][2] = cube[c][2][i];
					for(int i = 0 ; i < 3 ; i++) temp2[0][2-i] = cube[c][2-i][2];
					temp2[1][1] = cube[c][1][1];
					for(int y=  0 ; y < 3 ; y ++) {for(int x = 0 ; x< 3 ; x++) {cube[c][y][x] = temp2[y][x];}}
				
				}else if(methods[index].equals("F+")){
					c=2;
					for(int i = 0 ; i < 3 ; i++) temp2[i][2] = cube[c][0][i];
					for(int i = 0 ; i < 3 ; i++) temp2[2][2-i] = cube[c][i][2];
					for(int i = 0 ; i < 3 ; i++) temp2[2-i][0] = cube[c][2][2-i];
					for(int i = 0 ; i < 3 ; i++) temp2[0][i] = cube[c][2-i][0];
					temp2[1][1] = cube[c][1][1];
					
					
					for(int i = 0 ; i < 3 ; i++) temp[i] = cube[0][2][2-i];
					for(int i = 0 ; i < 3 ; i++) cube[0][2][2-i] = cube[4][i][2];
					for(int i = 0 ; i < 3 ; i++) cube[4][i][2] = cube[1][0][i];
					for(int i = 0 ; i < 3 ; i++) cube[1][0][i] = cube[5][2-i][0];
					for(int i = 0 ; i < 3 ; i++) cube[5][2-i][0] = temp[i];
					for(int y=  0 ; y < 3 ; y ++) {for(int x = 0 ; x< 3 ; x++) {cube[c][y][x] = temp2[y][x];}}
				
				}else if(methods[index].equals("F-")){
					c=2;
					for(int i = 0 ; i < 3 ; i++) temp[i] = cube[0][2][i];
					
					for(int i = 0 ; i < 3 ; i++) cube[0][2][i] = cube[5][i][0];
					for(int i = 0 ; i < 3 ; i++) cube[5][i][0] = cube[1][0][2-i];
					for(int i = 0 ; i < 3 ; i++) cube[1][0][2-i] = cube[4][2-i][2];
					for(int i = 0 ; i < 3 ; i++) cube[4][2-i][2] = temp[i];
					
					for(int i = 0 ; i < 3 ; i++) temp2[i][0] = cube[c][0][2-i];
					for(int i = 0 ; i < 3 ; i++) temp2[2][i] = cube[c][i][0];
					for(int i = 0 ; i < 3 ; i++) temp2[2-i][2] = cube[c][2][i];
					for(int i = 0 ; i < 3 ; i++) temp2[0][2-i] = cube[c][2-i][2];
					temp2[1][1] = cube[c][1][1];
					for(int y=  0 ; y < 3 ; y ++) {for(int x = 0 ; x< 3 ; x++) {cube[c][y][x] = temp2[y][x];}}
				
				}else if(methods[index].equals("B+")){
					c=3;
					for(int i = 0 ; i < 3 ; i++) temp2[i][2] = cube[c][0][i];
					for(int i = 0 ; i < 3 ; i++) temp2[2][2-i] = cube[c][i][2];
					for(int i = 0 ; i < 3 ; i++) temp2[2-i][0] = cube[c][2][2-i];
					for(int i = 0 ; i < 3 ; i++) temp2[0][i] = cube[c][2-i][0];
					temp2[1][1] = cube[c][1][1];
					
					for(int i = 0 ; i < 3 ; i++) temp[i] = cube[0][0][i];
					for(int i = 0 ; i < 3 ; i++) cube[0][0][i] = cube[5][i][2];
					for(int i = 0 ; i < 3 ; i++) cube[5][i][2] = cube[1][2][2-i];
					for(int i = 0 ; i < 3 ; i++) cube[1][2][2-i] = cube[4][2-i][0];
					for(int i = 0 ; i < 3 ; i++) cube[4][2-i][0] = temp[i];
					// 
					
					for(int y=  0 ; y < 3 ; y ++) {for(int x = 0 ; x< 3 ; x++) {cube[c][y][x] = temp2[y][x];}}
				
				}else if(methods[index].equals("B-")){
					c=3;
					for(int i = 0 ; i < 3 ; i++) temp[i] = cube[0][0][2-i];
					for(int i = 0 ; i < 3 ; i++) cube[0][0][2-i] = cube[4][i][0];
					for(int i = 0 ; i < 3 ; i++) cube[4][i][0] = cube[1][2][i];
					for(int i = 0 ; i < 3 ; i++) cube[1][2][i] = cube[5][2-i][2];
					for(int i = 0 ; i < 3 ; i++) cube[5][2-i][2] = temp[i];
					// 
					for(int i = 0 ; i < 3 ; i++) temp2[i][0] = cube[c][0][2-i];
					for(int i = 0 ; i < 3 ; i++) temp2[2][i] = cube[c][i][0];
					for(int i = 0 ; i < 3 ; i++) temp2[2-i][2] = cube[c][2][i];
					for(int i = 0 ; i < 3 ; i++) temp2[0][2-i] = cube[c][2-i][2];
					temp2[1][1] = cube[c][1][1];
					for(int y=  0 ; y < 3 ; y ++) {for(int x = 0 ; x< 3 ; x++) {cube[c][y][x] = temp2[y][x];}}
				}else if(methods[index].equals("L+")){
					
					c=4;
					for(int i = 0 ; i < 3 ; i++) temp2[i][2] = cube[c][0][i];
					for(int i = 0 ; i < 3 ; i++) temp2[2][2-i] = cube[c][i][2];
					for(int i = 0 ; i < 3 ; i++) temp2[2-i][0] = cube[c][2][2-i];
					for(int i = 0 ; i < 3 ; i++) temp2[0][i] = cube[c][2-i][0];
					temp2[1][1] = cube[c][1][1];
					//완료
					for(int i = 0 ; i < 3 ; i++) temp[2-i] = cube[0][2-i][0];
					for(int i = 0 ; i < 3 ; i++) cube[0][2-i][0] = cube[3][i][2];
					for(int i = 0 ; i < 3 ; i++) cube[3][i][2] = cube[1][2-i][0];
					for(int i = 0 ; i < 3 ; i++) cube[1][2-i][0] = cube[2][2-i][0];
					for(int i = 0 ; i < 3 ; i++) cube[2][2-i][0] = temp[2-i];
					
					
					for(int y=  0 ; y < 3 ; y ++) {for(int x = 0 ; x< 3 ; x++) {cube[c][y][x] = temp2[y][x];}}
				}else if(methods[index].equals("L-")){
					c=4;
					//완료
					for(int i = 0 ; i < 3 ; i++) temp[i] = cube[0][i][0];
					for(int i = 0 ; i < 3 ; i++) cube[0][i][0] = cube[2][i][0];
					for(int i = 0 ; i < 3 ; i++) cube[2][i][0] = cube[1][i][0];
					for(int i = 0 ; i < 3 ; i++) cube[1][i][0] = cube[3][2-i][2];
					for(int i = 0 ; i < 3 ; i++) cube[3][2-i][2] = temp[i];
					
					for(int i = 0 ; i < 3 ; i++) temp2[i][0] = cube[c][0][2-i];
					for(int i = 0 ; i < 3 ; i++) temp2[2][i] = cube[c][i][0];
					for(int i = 0 ; i < 3 ; i++) temp2[2-i][2] = cube[c][2][i];
					for(int i = 0 ; i < 3 ; i++) temp2[0][2-i] = cube[c][2-i][2];
					temp2[1][1] = cube[c][1][1];
					for(int y=  0 ; y < 3 ; y ++) {for(int x = 0 ; x< 3 ; x++) {cube[c][y][x] = temp2[y][x];}}
				
				}else if(methods[index].equals("R+")){
					c=5;
					// 완료
					for(int i = 0 ; i < 3 ; i++) temp2[i][2] = cube[c][0][i];
					for(int i = 0 ; i < 3 ; i++) temp2[2][2-i] = cube[c][i][2];
					for(int i = 0 ; i < 3 ; i++) temp2[2-i][0] = cube[c][2][2-i];
					for(int i = 0 ; i < 3 ; i++) temp2[0][i] = cube[c][2-i][0];
					temp2[1][1] = cube[c][1][1];
					
					for(int i = 0 ; i < 3 ; i++) temp[i] = cube[0][i][2];
					for(int i = 0 ; i < 3 ; i++) cube[0][i][2] = cube[2][i][2];
					for(int i = 0 ; i < 3 ; i++) cube[2][i][2] = cube[1][i][2];
					for(int i = 0 ; i < 3 ; i++) cube[1][i][2] = cube[3][2-i][0];
					for(int i = 0 ; i < 3 ; i++) cube[3][2-i][0] = temp[i];
					
					
					for(int y=  0 ; y < 3 ; y ++) {for(int x = 0 ; x< 3 ; x++) {cube[c][y][x] = temp2[y][x];}}
					
				}else if(methods[index].equals("R-")){
					c=5;
					
					for(int i = 0 ; i < 3 ; i++) temp[i] = cube[0][2-i][2];
					for(int i = 0 ; i < 3 ; i++) cube[0][2-i][2] = cube[3][i][0];
					for(int i = 0 ; i < 3 ; i++) cube[3][i][0] = cube[1][2-i][2];
					for(int i = 0 ; i < 3 ; i++) cube[1][2-i][2] = cube[2][2-i][2];
					for(int i = 0 ; i < 3 ; i++) cube[2][2-i][2] = temp[i];
					
					for(int i = 0 ; i < 3 ; i++) temp2[i][0] = cube[c][0][2-i];
					for(int i = 0 ; i < 3 ; i++) temp2[2][i] = cube[c][i][0];
					for(int i = 0 ; i < 3 ; i++) temp2[2-i][2] = cube[c][2][i];
					for(int i = 0 ; i < 3 ; i++) temp2[0][2-i] = cube[c][2-i][2];
					temp2[1][1] = cube[c][1][1];
					for(int y=  0 ; y < 3 ; y ++) {for(int x = 0 ; x< 3 ; x++) {cube[c][y][x] = temp2[y][x];}}
				}
			}
			for(int y = 0 ;  y< 3 ; y++) {
				for(int x = 0 ; x < 3 ; x++) {
					sb.append(cube[0][y][x]);
				}
				sb.append("\n");
			}
		}
		System.out.print(sb);
	}
}

728x90

'구현,시뮬' 카테고리의 다른 글

BOJ - 3020 ) 개똥벌레  (1) 2021.11.03
BOJ - 5373 ) 큐빙  (0) 2021.10.02
[JAVA] Programmers 위클리 챌린지 1~8주 풀이  (1) 2021.09.30
BOJ - 1120 ) 문자열  (0) 2021.09.13
BOJ - 2417 ) 정수제곱근  (0) 2021.08.01
BOJ - 11728 ) 배열 합치기  (0) 2021.07.29
0 Comments
댓글쓰기 폼