魔方可以对它的6个面自由旋转。
我们来操作一个2阶魔方(如图1所示):
为了描述方便,我们为它建立了坐标系。
各个面的初始状态如下:
x轴正向:绿
x轴反向:蓝
y轴正向:红
y轴反向:橙
z轴正向:白
z轴反向:黄
假设我们规定,只能对该魔方进行3种操作。分别标记为:
x 表示在x轴正向做顺时针旋转
y 表示在y轴正向做顺时针旋转
z 表示在z轴正向做顺时针旋转
xyz 则表示顺序执行x,y,z 3个操作
题目的要求是:
从标准输入获得一个串,表示操作序列。
程序输出:距离我们最近的那个小方块的3个面的颜色。
顺序是:x面,y面,z面。
例如:在初始状态,应该输出:
绿红白
初始状态下,如果用户输入:
x
则应该输出:
绿白橙
初始状态下,如果用户输入:
zyx
则应该输出:
红白绿
请编程实现所述功能。
import java.util.Scanner;
public class Main {
public int[] Now = {1, 1, 1, 1}; //正前方
public int[] Back = {2, 2, 2, 2};
public int[] Left = {3, 3, 3, 3};
public int[] Right = {4, 4, 4, 4};
public int[] Up = {5, 5, 5, 5};
public int[] Down = {6, 6, 6, 6};
public void reverseX() {
int[] value = new int[8];
value[0] = Up[2];
value[1] = Up[3];
value[2] = Right[0];
value[3] = Right[2];
value[4] = Down[2];
value[5] = Down[3];
value[6] = Left[3];
value[7] = Left[0];
Up[2] = value[6];
Up[3] = value[7];
Right[0] = value[0];
Right[2] = value[1];
Down[2] = value[2];
Down[3] = value[3];
Left[3] = value[4];
Left[0] = value[5];
value[0] = Now[0];
value[1] = Now[1];
value[2] = Now[2];
value[3] = Now[3];
Now[0] = value[3];
Now[1] = value[0];
Now[2] = value[1];
Now[3] = value[2];
}
public void reverseY() {
int[] value = new int[8];
value[0] = Up[3];
value[1] = Up[1];
value[2] = Back[1];
value[3] = Back[2];
value[4] = Down[1];
value[5] = Down[2];
value[6] = Now[2];
value[7] = Now[1];
Up[3] = value[6];
Up[1] = value[7];
Back[1] = value[0];
Back[2] = value[1];
Down[1] = value[2];
Down[2] = value[3];
Now[2] = value[4];
Now[1] = value[5];
value[0] = Right[0];
value[1] = Right[1];
value[2] = Right[2];
value[3] = Right[3];
Right[0] = value[2];
Right[1] = value[0];
Right[2] = value[3];
Right[3] = value[1];
}
public void reverseZ() {
int[] value = new int[8];
value[0] = Now[1];
value[1] = Now[0];
value[2] = Left[0];
value[3] = Left[1];
value[4] = Back[0];
value[5] = Back[1];
value[6] = Right[1];
value[7] = Right[0];
Now[1] = value[6];
Now[0] = value[7];
Left[0] = value[0];
Left[1] = value[1];
Back[0] = value[2];
Back[1] = value[3];
Right[1] = value[4];
Right[0] = value[5];
value[0] = Up[0];
value[1] = Up[1];
value[2] = Up[2];
value[3] = Up[3];
Up[0] = value[2];
Up[1] = value[0];
Up[2] = value[3];
Up[3] = value[1];
}
public void getResult(String A) {
String[] color = {"", "绿", "蓝", "橙", "红", "白", "黄"};
for(int i = 0;i < A.length();i++) {
char j = A.charAt(i);
if(j == 'x')
reverseX();
else if(j == 'y')
reverseY();
else if(j == 'z')
reverseZ();
}
int[] result = new int[3];
result[0] = Now[1];
result[1] = Right[0];
result[2] = Up[3];
for(int i = 0;i < 3;i++)
System.out.print(color[result[i]]);
}
public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
String A = in.next();
test.getResult(A);
}
}