二階魔方 枚舉

魔方可以對它的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 則應該輸出: 紅白綠






#include <iostream>
using namespace std;
string cl[] = {"綠","藍","紅","橙","白","黃"}; 
char ss[] = "111122223333444455556666";
//
//枚舉x,y,z軸順時針操作
//x,y,z後第一個數表示哪個面(0-5)
//x 0 - 1
//y 2 - 3
//z 4 - 5
void xx(char * s){
	char t1, t2, t3;
	char &x00 = s[0*4 + 0], &x01 = s[0*4 + 1], &x02 = s[0*4 + 2], &x03 = s[0*4 + 3];
	char &y20 = s[2*4 + 0],&y22 = s[2*4 + 2];
	char &z43 = s[4*4 + 3],&z42 = s[4*4 + 2];
	char &y31 = s[3*4 + 1],&y33 = s[3*4 + 3];
	char &z51 = s[5*4 + 1],&z50 = s[5*4 + 0];
	t1 = y20; t2 = y22; t3 = x00;
	y20 = z42; y22 = z43;
	z42 = y33; z43 = y31;
	y31 = z50; y33 = z51;
	z50 = t2;
	z51 = t1;
	x00 = x02; x02 = x03; x03 = x01; x01 = t3;
}
void yy(char *s){
	char t1, t2, t3;
	char &y20 = s[2*4 + 0], &y21 = s[2*4 + 1], &y22 = s[2*4 + 2], &y23 = s[2*4 + 3];
	char &x10 = s[1*4 + 0], &x12 = s[1*4 + 2];
	char &z43 = s[4*4 + 3], &z41 = s[4*4 + 1];
	char &x01 = s[0*4 + 1], &x03 = s[0*4 + 3];
	char &z51 = s[5*4 + 1], &z53 = s[5*4 + 3];
	t1 = x10, t2 = x12, t3 = y20;
	x10 = z43; x12 = z41;
	z41 = x01; z43 = x03;
	x01 = z51; x03 = z53;
	z51 = t2; z53 = t1; 
	y20 = y22;
	y22 = y23;
	y23 = y21;
	y21 = t3;
}
void zz(char *s){
	char t1, t2, t3;
	char &z40 = s[4*4 + 0], &z41 = s[4*4 + 1], &z42 = s[4*4 + 2], &z43 = s[4*4 + 3];
	char &x00 = s[0*4 + 0], &x01 = s[0*4 + 1];
	char &y20 = s[2*4 + 0], &y21 = s[2*4 + 1];
	char &x10 = s[1*4 + 0], &x11 = s[1*4 + 1];
	char &y31 = s[3*4 + 1], &y30 = s[3*4 + 0];
	t1 = x00; t2 = x01; t3 = z40;
	x00 = y20; x01 = y21;
	y20 = x10; y21 = x11;
	x10 = y30; x11 = y31;
	y30 = t1; y31 = t2;
	z40 = z42; z42 = z43; z43 = z41; z41 = t3;
}

int main(){
	
	string s;
	cin >> s;
	cout << s << endl;
	for(int i = 0; i < s.length(); i++){
 		if(s[i] == 'x'){
			xx(ss);	
		}
		else if(s[i] == 'y'){
			yy(ss);
		}
		else if(s[i] == 'z'){
			zz(ss);
		}
		cout << s[i] << endl;
		cout << ss << endl;
	}
	cout << cl[ss[0*4 + 1] -'0'- 1];
	cout << cl[ss[2*4 + 0] -'0'- 1];
	cout << cl[ss[4*4 + 3] -'0'- 1];
}
/*
		16 17
		19 18
		-----
	12 13  |0  1 | 4 5 | 8 9
	15 14  |3  2 | 7 6 | 11 10
		-----
		20 21
		23 22

x 操作 (0, 1, 2, 3) (4, 21, 14, 19) (7, 20, 13, 18)
y 操作 (4, 5, 6, 7) (1, 17, 11, 21) (2, 18, 8, 22)
z 操作 (16, 17, 18, 19) (1, 13, 9, 5) (0, 12, 8, 4) 
*/
#include <iostream>
using namespace std;
char cub[25] = "111133332222444455556666";
int transx[][4] = {{0, 1, 2, 3}, {4, 21, 14, 19}, {7, 20, 13, 18}};
int transy[][4] = {{4, 5, 6, 7}, {1, 17, 11, 21}, {2, 18, 8, 22}};
int transz[][4] = {{16, 17, 18, 19}, {1, 13, 9, 5}, {0, 12, 8, 4}};
string color[7] = {"", "綠", "藍", "紅", "橙", "白", "黃"};
void f(char * a, int trans[][4]){
	for(int i = 0; i < 3; i++){
		char t = a[trans[i][3]];
		a[trans[i][3]] = a[trans[i][2]];
		a[trans[i][2]] = a[trans[i][1]];
		a[trans[i][1]] = a[trans[i][0]];
		a[trans[i][0]] = t;
	}
}
int main(){
	string s;
	cin >> s;
	for(int i = 0; i < s.length(); i++){
		switch(s[i]){
			case 'x':
				f(cub, transx);
				break;
			case 'y':
				f(cub, transy);
				break;
			case 'z':
				f(cub, transz);
				break;
		}
	}
	cout << color[cub[1]-'0'] << color[cub[4]-'0'] << color[cub[18]-'0'] << endl;	
}




發佈了17 篇原創文章 · 獲贊 3 · 訪問量 9254
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章