[백준] 5373 큐브

문제보기

그냥 하나하나 다 경우의 수마다 하드코딩… ㅠㅠ

더 좋은 방법이 없을까??? 스케일이 커져도 적용가능한 ?

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

public class Main {
	
	static char[][] uu;
	static char[][] dd;
	static char[][] ff;
	static char[][] bb;
	static char[][] ll;
	static char[][] rr;
	
	static char[][] rotate(char[][] map, char dir) {
		char[][] temp = new char[3][3];
		
		if(dir == '-') {
			for(int i = 0 ; i < 3 ; i++ ) {
				temp[2][i] = map[i][0];
				temp[1][i] = map[i][1];
				temp[0][i] = map[i][2];
			}
		}else {
			for(int i = 0 ; i < 3 ; i++ ) {
				temp[0][2-i] = map[i][0];
				temp[1][2-i] = map[i][1];
				temp[2][2-i] = map[i][2];
			}
		}
		return temp;
		
	}
	
	static void go(String s) {
		char obj = s.charAt(0);
		char dir = s.charAt(1);
		
		
		if(obj == 'L') {
			
			ll = rotate(ll, dir);
			char[] temp = new char[3];
			
			if(dir == '-') {
				for(int i = 0 ; i < 3 ; i++) {
					temp[i] = uu[i][0];
					uu[i][0] = ff[i][0];
					ff[i][0] = dd[i][0];
					dd[i][0] = bb[i][0];
					bb[i][0] = temp[i];
				}
			}else {
				for(int i = 0 ; i < 3 ; i++) {
					temp[i] = uu[i][0];
					uu[i][0] = bb[i][0];
					bb[i][0] = dd[i][0];
					dd[i][0] = ff[i][0];
					ff[i][0] = temp[i];
				}
			}
					
		}else if(obj == 'R') {
			
			rr = rotate(rr, dir);
			char[] temp = new char[3];
			
			if(dir == '-') {
				for(int i = 0 ; i < 3 ; i++) {
					temp[i] = uu[i][2];
					uu[i][2] = bb[i][2];
					bb[i][2] = dd[i][2];
					dd[i][2] = ff[i][2];
					ff[i][2] = temp[i];
				}
			}else {
				for(int i = 0 ; i < 3 ; i++) {
					temp[i] = uu[i][2];
					uu[i][2] = ff[i][2];
					ff[i][2] = dd[i][2];
					dd[i][2] = bb[i][2];
					bb[i][2] = temp[i];
					
				}
			}		
		}else if(obj == 'F') {		
			ff = rotate(ff, dir);
			char[] temp = new char[3];
			
			if(dir == '-') {
				for(int i = 0 ; i < 3 ; i++) {
					temp[i] = uu[2][i];
					uu[2][i] = rr[i][0];
					rr[i][0] = dd[0][2-i];
					dd[0][2-i] = ll[2-i][2];
					ll[2-i][2] = temp[i];
				}
			}else {
				for(int i = 0 ; i < 3 ; i++) {
					temp[i] = uu[2][i];
					uu[2][i] = ll[2-i][2];
					ll[2-i][2] = dd[0][2-i];
					dd[0][2-i] = rr[i][0];
					rr[i][0] = temp[i];
				}
			}		
		}else if(obj == 'B') {
			bb = rotate(bb, dir);
			char[] temp = new char[3];
			
			if(dir == '-') {
				for(int i = 0 ; i < 3 ; i++) {
					temp[i] = uu[0][i];
					uu[0][i] = ll[2-i][0];
					ll[2-i][0] = dd[2][2-i];
					dd[2][2-i] = rr[i][2];
					rr[i][2] = temp[i];
				}
			}else {
				for(int i = 0 ; i < 3 ; i++) {
					temp[i] = uu[0][i];
					uu[0][i] = rr[i][2];
					rr[i][2] = 	dd[2][2-i];
					dd[2][2-i] = ll[2-i][0];
					ll[2-i][0] = temp[i];
				}
			}	
		}else if(obj == 'U') {
			uu = rotate(uu, dir);
			char[] temp = new char[3];
			
			if(dir == '-') {
				for(int i = 0 ; i < 3 ; i++) {
					temp[i] = ff[0][i];
					ff[0][i] = ll[0][i];
					ll[0][i] = bb[2][2-i];
					bb[2][2-i] = rr[0][i];
					rr[0][i] = temp[i];
				}
			}else {
				for(int i = 0 ; i < 3 ; i++) {
					temp[i] = ff[0][i];
					ff[0][i] = rr[0][i];
					rr[0][i] = bb[2][2-i];
					bb[2][2-i] = ll[0][i];
					ll[0][i] = temp[i];
				}
			}
		}else if(obj == 'D') {
			dd = rotate(dd, dir);
			char[] temp = new char[3];
			
			if(dir == '-') {
				for(int i = 0 ; i < 3 ; i++) {
					temp[i] = ff[2][i];
					ff[2][i] = rr[2][i];
					rr[2][i] = bb[0][2-i];
					bb[0][2-i] = ll[2][i];
					ll[2][i] = temp[i];
					
				}
			}else {
				for(int i = 0 ; i < 3 ; i++) {
					temp[i] = ff[2][i];
					ff[2][i] = ll[2][i];
					ll[2][i] = bb[0][2-i];
					bb[0][2-i] = rr[2][i];
					rr[2][i] = temp[i];
				}
			}
		}
		
	}
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		StringBuilder sb = new StringBuilder();
		while(t-->0) {
			uu = new char[3][3];
			dd = new char[3][3];
			ff = new char[3][3];
			bb = new char[3][3];
			ll = new char[3][3];
			rr = new char[3][3];
			for(int i = 0 ; i < 3 ; i++) {
				Arrays.fill(uu[i], 'w');
				Arrays.fill(dd[i], 'y');
				Arrays.fill(ff[i], 'r');
				Arrays.fill(bb[i], 'o');
				Arrays.fill(ll[i], 'g');
				Arrays.fill(rr[i], 'b');
			}
			
			int n = Integer.parseInt(br.readLine());
			StringTokenizer st = new StringTokenizer(br.readLine());
			for(int i = 0 ; i < n ; i++) {
				go(st.nextToken());
			}
			for(int i = 0 ; i < 3 ; i++) {
				for(int j = 0 ; j < 3 ;j++) {
					sb.append(uu[i][j]);
				}sb.append("\n");
			}
			
		}
		System.out.println(sb);
	}
}

Tags:

Categories:

Updated:

Comments