POJ 1955 Rubik's Cube

1550人阅读 评论(0)

vim写代码真费事，手都写酸了。。。

Rubik's Cube
 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 638 Accepted: 324

Description

Background
Rummaging through the stuff of your childhood you find an old toy which you identify as the famous Rubik's Cube. While playing around with it you have to acknowledge that throughout the years your ability to solve the puzzle has not improved a bit. But because you always wanted to understand the thing and the only other thing you could do right now is to prepare for an exam, you decide to give it a try. Luckily the brother of your girlfriend is an expert and able to fix the cube no matter how messed-up it is. The problem is that he stays with his girlfriend in the Netherlands most of the time, so you need a solution for long-distance learning. You decide to implement a program which is able to document the state of the cube and the turns to be made.
Problem
A Rubik's Cube is covered with 54 square areas called facelets, 9 facelets on each of its six sides. Each facelet has a certain color. Usually when the cube is in its starting state, all facelets belonging to one side have the same color. For the original cube these are red, yellow, green, blue, white and orange.

The positions of the facelets can be changed by turning the sides of the cube. This moves nine "little cubes" together with their attached facelets into a new position (see Fig. 1).
The problem is to determine how the facelets of the entire cube are colored after turning different sides in different directions.

Input

The first line contains the number of scenarios. Each scenario consists of two sections. The first section describes the starting state of the cube and the second describes the turns to be made.
The starting state describes the colors of the facelets and where they are positioned. The colors are identified by single characters, and one character is given per facelet. Characters are separated by blanks and arranged in a certain pattern (see Fig. 2). The pattern identifies all six sides of the cube and can be thought of as a folding pattern. As shown in Fig. 2, the description of the top side of the cube is placed right over the description of the front side. This is done by indenting the lines with blanks. The next three lines contain the descriptions of the left, front, right and back side as shown in Fig. 2. The descriptions are simply concatenated with a blank character used as separator. After that the description of the bottom side follows, using the same format as the one used to describe the top side. This concludes the description of the starting state.
Then follows the second section of the scenario containing the turns which have to be performed. The description of the turns starts with a line containing the number of turns t (t > 0). Each turn is given in a separate line and consists of two integer values s and d which are separated by a single blank. The first value s determines the side of the cube which has to be turned. The sides are serially numbered as follows:left '0', front '1', right '2', back '3', top '4', bottom '5'. The second value d determines in which direction

the side s has to be turned and can either be '1' or '-1'. A '1' stands for clockwise and a '-1' for counterclockwise.The direction is given under the assumption that the viewer is looking directly at the specific side of the cube.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. After this line print the resulting state of the cube using the same format as the input. Each scenario is terminated by a single blank line.

Sample Input

2
w w w
w w w
w w w
r r r g g g b b b o o o
r r r g g g b b b o o o
r r r g g g b b b o o o
y y y
y y y
y y y
2
3 1
0 -1
g b b
g w w
g w w
r r r y g g b b y o o w
r r r y g g b b y o o w
w w w r g g b b y o o b
o y y
o y y
o r r
2
0 1
3 -1


Sample Output

Scenario #1:
g b b
g w w
g w w
r r r y g g b b y o o w
r r r y g g b b y o o w
w w w r g g b b y o o b
o y y
o y y
o r r

Scenario #2:
w w w
w w w
w w w
r r r g g g b b b o o o
r r r g g g b b b o o o
r r r g g g b b b o o o
y y y
y y y
y y y


Source

TUD Programming Contest 2002, Darmstadt, Germany

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int mofang[60];

void SHOW()
{
int cnt=1;printf("      ");
for(int i=0;i<9;i++,cnt++)
{
printf("%c ",mofang[i]);
if(cnt%3==0)
{
if(i!=0) putchar(10);
if(i!=8) printf("      ");
}
}
cnt=1;
for(int i=9;i<45;i++,cnt++)
{
printf("%c ",mofang[i]);
if(cnt%12==0)
putchar(10);
}
cnt=1; printf("      ");
for(int i=45;i<54;i++,cnt++)
{
printf("%c ",mofang[i]);
if(cnt%3==0)
{
putchar(10);
if(i!=53) printf("      ");
}
}
putchar(10);
}

void quanCLOCKwise(int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8,int a9)
{
int a,b,c;
a=mofang[a1]; b=mofang[a2]; c=mofang[a3];
mofang[a1]=mofang[a7]; mofang[a2]=mofang[a4]; mofang[a3]=a;
mofang[a7]=mofang[a9]; mofang[a4]=mofang[a8]; mofang[a8]=mofang[a6];
mofang[a6]=b; mofang[a9]=c;
}

void quanFANCLOCKwise(int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8,int a9)
{
int a=mofang[a1],b=mofang[a2],c=mofang[a3];
mofang[a1]=c; mofang[a2]=mofang[a6]; mofang[a3]=mofang[a9];
mofang[a6]=mofang[a8];mofang[a9]=mofang[a7];
mofang[a7]=a; mofang[a8]=mofang[a4];mofang[a4]=b;
}

void huanCLOCKwise(int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8,int a9,int a10,int a11,int a12)
{
int a=mofang[a1],b=mofang[a2],c=mofang[a3];
mofang[a1]=mofang[a10]; mofang[a2]=mofang[a11];mofang[a3]=mofang[a12];
mofang[a10]=mofang[a7];mofang[a11]=mofang[a8];mofang[a12]=mofang[a9];
mofang[a9]=mofang[a6];mofang[a8]=mofang[a5];mofang[a7]=mofang[a4];
mofang[a6]=c; mofang[a5]=b; mofang[a4]=a;
}

void huanFANCLOCKwise (int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8,int a9,int a10,int a11,int a12)
{
int a=mofang[a1],b=mofang[a2],c=mofang[a3];
mofang[a1]=mofang[a4]; mofang[a2]=mofang[a5]; mofang[a3]=mofang[a6];
mofang[a4]=mofang[a7]; mofang[a5]=mofang[a8]; mofang[a6]=mofang[a9];
mofang[a9]=mofang[a12]; mofang[a8]=mofang[a11]; mofang[a7]=mofang[a10];
mofang[a10]=a; mofang[a11]=b; mofang[a12]=c;
}

void QianClockwise()
{
quanCLOCKwise(12,13,14,24,25,26,36,37,38);
huanCLOCKwise(6,7,8,15,27,39,47,46,45,35,23,11);
}

void QianFanClockwise()
{
quanFANCLOCKwise(12,13,14,24,25,26,36,37,38);
huanFANCLOCKwise(6,7,8,15,27,39,47,46,45,35,23,11);
}

void BackClockwise()
{
quanCLOCKwise(18,19,20,30,31,32,42,43,44);
huanCLOCKwise(2,1,0,9,21,33,51,52,53,41,29,17);
}

void BackFanClockwise()
{
quanFANCLOCKwise(18,19,20,30,31,32,42,43,44);
huanFANCLOCKwise(2,1,0,9,21,33,51,52,53,41,29,17);
}

void LeftClockwise()
{
quanCLOCKwise(9,10,11,21,22,23,33,34,35);
huanCLOCKwise(0,3,6,12,24,36,45,48,51,44,32,20);
}

void LeftFanClockwise()
{
quanFANCLOCKwise(9,10,11,21,22,23,33,34,35);
huanFANCLOCKwise(0,3,6,12,24,36,45,48,51,44,32,20);
}

void RightClockwise()
{
quanCLOCKwise(15,16,17,27,28,29,39,40,41);
huanCLOCKwise(8,5,2,18,30,42,53,50,47,38,26,14);
}

void RightFanClockwise()
{
quanFANCLOCKwise(15,16,17,27,28,29,39,40,41);
huanFANCLOCKwise(8,5,2,18,30,42,53,50,47,38,26,14);
}

void TopClockwise()
{
quanCLOCKwise(0,1,2,3,4,5,6,7,8);
huanCLOCKwise(20,19,18,17,16,15,14,13,12,11,10,9);
}

void TopFanClockwise()
{
quanFANCLOCKwise(0,1,2,3,4,5,6,7,8);
huanFANCLOCKwise(20,19,18,17,16,15,14,13,12,11,10,9);
}

void BotClockwise()
{
quanCLOCKwise(45,46,47,48,49,50,51,52,53);
huanCLOCKwise(36,37,38,39,40,41,42,43,44,33,34,35);
}

void BotFanClockwise()
{
quanFANCLOCKwise(45,46,47,48,49,50,51,52,53);
huanFANCLOCKwise(36,37,38,39,40,41,42,43,44,33,34,35);
}

int main()
{
int t,cas=1;
scanf("%d",&t);
while(t--)
{
printf("Scenario #%d:\n",cas++);
for(int i=0;i<54;i++)
{
char c[3];
scanf("%s",c);
mofang[i]=(int)c[0];
}
int m;
scanf("%d",&m);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
if(a==0)
{
if(b==1) LeftClockwise();
else LeftFanClockwise();
}
else if(a==1)
{
if(b==1) QianClockwise();
else QianFanClockwise();
}
else if(a==2)
{
if(b==1) RightClockwise();
else RightFanClockwise();
}
else if(a==3)
{
if(b==1) BackClockwise();
else BackFanClockwise();
}
else if(a==4)
{
if(b==1) TopClockwise();
else TopFanClockwise();
}
else if(a==5)
{
if(b==1) BotClockwise();
else BotFanClockwise();
}
}
SHOW();
}
return 0;
}


1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：885398次
• 积分：17760
• 等级：
• 排名：第550名
• 原创：934篇
• 转载：9篇
• 译文：0篇
• 评论：111条
阅读排行
最新评论