POJ 1955 -- Rubik's Cube
2018-02-03 21:53
190 查看
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.
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.
这个题就是个纯模拟题,花了我一天的时间。
仔细想一想,魔方一共有6个面,转每个面可以看成是转一个面,首先就要把每个面表示出来,用指针,在函数中可以改每个面的数。 分别把每个面当成正面,上下左右重新弄一下,然后进入顺时针或者逆时针的函数。由于指定一个面是正面,其他的面不是简单的平移关系,这时候就要把正面周围的面找出来,luckily,我们只要找和正面周围的一行或一列就行,所以找其他的面时,只要改变与正面有关系的地方就行了完事之后就变回来,
用的指针 (*p)[4] 这样 [3][4];
#include<iostream>
#include<cstdio>
using namespace std;
char sa[10][10],sb[10][10],sc[10][10];
void change0( char (*to)[5], char (*bo)[5] )
{
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
sa[i][j] = to[i][j];
sb[i][j] = bo[i][j];
}
for (int i = 1; i <= 3; i++)
{
to[3][i] = sa[i][1];
bo[1][i] = sb[4-i][1];
}
return;
}
void dischange0( char (*to)[5], char (*bo)[5] )
{
for (int i = 1; i <= 3; i++)
{
sa[i][1] = to[3][i];
sb[i][1] = bo[1][4-i];
}
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
to[i][j] = sa[i][j];
bo[i][j] = sb[i][j];
}
return;
}
void change2( char (*to)[5], char (*bo)[5] )
{
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
sa[i][j] = to[i][j];
sb[i][j] = bo[i][j];
}
for (int i = 1; i <= 3; i++)
{
to[3][i] = sa[4-i][3];
bo[1][i] = sb[i][3];
}
return;
}
void dischange2( char (*to)[5], char (*bo)[5] )
{
for (int i = 1; i <= 3; i++)
{
sa[i][3] = to[3][4-i];
sb[i][3] = bo[1][i];
}
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
to[i][j] = sa[i][j];
bo[i][j] = sb[i][j];
}
return;
}
void change3( char (*to)[5], char (*bo)[5] )
{
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
sa[i][j] = to[i][j];
sb[i][j] = bo[i][j];
}
for (int i = 1; i <= 3; i++)
{
to[3][i] = sa[1][4-i];
bo[1][i] = sb[3][4-i];
}
return;
}
void dischange3( char (*to)[5], char (*bo)[5] )
{
for (int i = 1; i <= 3; i++)
{
sa[1][i] = to[3][4-i];
sb[3][i] = bo[1][4-i];
}
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
to[i][j] = sa[i][j];
bo[i][j] = sb[i][j];
}
return;
}
void change4(char (*le)[5], char (*ri)[5], char (*ba)[5])
{
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
sa[i][j] = le[i][j];
sb[i][j] = ri[i][j];
sc[i][j] = ba[i][j];
}
for (int i = 1; i <= 3; i++)
{
le[i][3] = sa[1][i];
ri[i][1] = sb[1][4-i];
ba[3][i] = sc[1][4-i];
}
return;
}
void dischange4(char (*le)[5], char (*ri)[5], char (*ba)[5])
{
for (int i = 1; i <= 3; i++)
{
sa[1][i] = le[i][3];
sb[1][i] = ri[4-i][1];
sc[1][i] = ba[3][4-i];
}
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
le[i][j] = sa[i][j];
ba[i][j] = sc[i][j];
ri[i][j] = sb[i][j];
}
return;
}
void change5(char (*le)[5], char (*ri)[5], char (*ba)[5])
{
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
sa[i][j] = le[i][j];
sb[i][j] = ri[i][j];
sc[i][j] = ba[i][j];
}
for (int i = 1; i <= 3; i++)
{
le[i][3] = sa[3][4-i];
ri[i][1] = sb[3][i];
ba[1][i] = sc[3][4-i];
}
return;
}
void dischange5(char (*le)[5], char (*ri)[5], char (*ba)[5])
{
for (int i = 1; i <= 3; i++)
{
sa[3][i] = le[4-i][3];
sb[3][i] = ri[i][1];
sc[3][i] = ba[1][4-i];
}
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
le[i][j] = sa[i][j];
ri[i][j] = sb[i][j];
ba[i][j] = sc[i][j];
}
return;
}
void clockwise(char (*fr)[5],char (*le)[5], char (*ri)[5], char (*to)[5], char (*bo)[5])
{
char s[10][10];
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
s[i][j] = fr[i][j];
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
fr[j][4-i] = s[i][j];
char x,y,z;
x = le[1][3];
y = le[2][3];
z = le[3][3];
for (int i = 1; i <= 3; i++)
le[i][3] = bo[1][i];
for (int i = 1; i <= 3; i++)
bo[1][i] = ri[4-i][1];
for (int i = 1; i <= 3; i++)
ri[i][1] = to[3][i];
to[3][1] = z;
to[3][2] = y;
to[3][3] = x;
return;
}
void counterclockwise(char (*fr)[5],char (*le)[5], char (*ri)[5], char (*to)[5], char (*bo)[5])
{
char s[10][10];
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
s[i][j] = fr[i][j];
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
fr[4-j][i] = s[i][j];
char x,y,z;
x = le[1][3];
y = le[2][3];
z = le[3][3];
for (int i = 1; i <= 3; i++)
le[i][3] = to[3][4-i];
for (int i = 1; i <= 3; i++)
to[3][i] = ri[i][1];
for (int i = 1; i <= 3; i++)
ri[i][1] = bo[1][4-i];
bo[1][1] = x;
bo[1][2] = y;
bo[1][3] = z;
return;
}
int main()
{
char fr[10][5],ba[10][5],le[10][5],ri[10][5],to[10][5],bo[10][5];
char map[100][100];
int n,m,t,k,x,y;
//freopen("yu.in","r",stdin);
//freopen("yu.out","w",stdout);
scanf("%d",&t);
k = 0;
while(t--)
{
for (int i = 0; i <= 9; i++)
cin.getline(map[i],100);
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
to[i][j] = map[i][2*j+4];
le[i][j] = map[3+i][2*j-2];
fr[i][j] = map[3+i][4+j*2];
ri[i][j] = map[3+i][10+2*j];
ba[i][j] = map[3+i][16+2*j];
bo[i][j] = map[6+i][4+j*2];
}
scanf("%d",&n);
for (int i = 1; i <= n; i++)
{
scanf("%d%d",&x,&y);
switch (x)
{
case 0 : change0(to,bo); if (y == 1) clockwise(le,ba,fr,to,bo); else
counterclockwise(le,ba,fr,to,bo); dischange0(to,bo); break;
case 1 : if (y == 1) clockwise(fr,le,ri,to,bo); else
counterclockwise(fr,le,ri,to,bo); break;
case 2 : change2(to,bo);if (y == 1) clockwise(ri,fr,ba,to,bo); else
counterclockwise(ri,fr,ba,to,bo); dischange2(to,bo); break;
case 3 : change3(to,bo);if (y == 1) clockwise(ba,ri,le,to,bo); else
counterclockwise(ba,ri,le,to,bo); dischange3(to,bo); break;
case 4 : change4(le,ri,ba);if (y == 1) clockwise(to,le,ri,ba,fr); else
counterclockwise(to,le,ri,ba,fr); dischange4(le,ri,ba); break;
case 5 : change5(le,ri,ba);if (y == 1) clockwise(bo,le,ri,fr,ba); else
counterclockwise(bo,le,ri,fr,ba); dischange5(le,ri,ba); break;
}
}
k++;
printf("Scenario #%d:\n",k);
for (int i = 1; i <= 3; i++)
printf(" %c %c %c\n",to[i][1],to[i][2],to[i][3]);
for (int i = 1; i <= 3; i++)
printf("%c %c %c %c %c %c %c %c %c %c %c %c\n",le[i][1],le[i][2],le[i][3],fr[i][1],fr[i][2],fr[i][3],ri[i][1],ri[i][2],ri[i][3],ba[i][1],ba[i][2],ba[i][3]);
for (int i = 1; i <= 3; i++)
printf(" %c %c %c\n",bo[i][1],bo[i][2],bo[i][3]);
printf("\n");
}
return 0;
}
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.
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.
这个题就是个纯模拟题,花了我一天的时间。
仔细想一想,魔方一共有6个面,转每个面可以看成是转一个面,首先就要把每个面表示出来,用指针,在函数中可以改每个面的数。 分别把每个面当成正面,上下左右重新弄一下,然后进入顺时针或者逆时针的函数。由于指定一个面是正面,其他的面不是简单的平移关系,这时候就要把正面周围的面找出来,luckily,我们只要找和正面周围的一行或一列就行,所以找其他的面时,只要改变与正面有关系的地方就行了完事之后就变回来,
用的指针 (*p)[4] 这样 [3][4];
#include<iostream>
#include<cstdio>
using namespace std;
char sa[10][10],sb[10][10],sc[10][10];
void change0( char (*to)[5], char (*bo)[5] )
{
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
sa[i][j] = to[i][j];
sb[i][j] = bo[i][j];
}
for (int i = 1; i <= 3; i++)
{
to[3][i] = sa[i][1];
bo[1][i] = sb[4-i][1];
}
return;
}
void dischange0( char (*to)[5], char (*bo)[5] )
{
for (int i = 1; i <= 3; i++)
{
sa[i][1] = to[3][i];
sb[i][1] = bo[1][4-i];
}
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
to[i][j] = sa[i][j];
bo[i][j] = sb[i][j];
}
return;
}
void change2( char (*to)[5], char (*bo)[5] )
{
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
sa[i][j] = to[i][j];
sb[i][j] = bo[i][j];
}
for (int i = 1; i <= 3; i++)
{
to[3][i] = sa[4-i][3];
bo[1][i] = sb[i][3];
}
return;
}
void dischange2( char (*to)[5], char (*bo)[5] )
{
for (int i = 1; i <= 3; i++)
{
sa[i][3] = to[3][4-i];
sb[i][3] = bo[1][i];
}
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
to[i][j] = sa[i][j];
bo[i][j] = sb[i][j];
}
return;
}
void change3( char (*to)[5], char (*bo)[5] )
{
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
sa[i][j] = to[i][j];
sb[i][j] = bo[i][j];
}
for (int i = 1; i <= 3; i++)
{
to[3][i] = sa[1][4-i];
bo[1][i] = sb[3][4-i];
}
return;
}
void dischange3( char (*to)[5], char (*bo)[5] )
{
for (int i = 1; i <= 3; i++)
{
sa[1][i] = to[3][4-i];
sb[3][i] = bo[1][4-i];
}
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
to[i][j] = sa[i][j];
bo[i][j] = sb[i][j];
}
return;
}
void change4(char (*le)[5], char (*ri)[5], char (*ba)[5])
{
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
sa[i][j] = le[i][j];
sb[i][j] = ri[i][j];
sc[i][j] = ba[i][j];
}
for (int i = 1; i <= 3; i++)
{
le[i][3] = sa[1][i];
ri[i][1] = sb[1][4-i];
ba[3][i] = sc[1][4-i];
}
return;
}
void dischange4(char (*le)[5], char (*ri)[5], char (*ba)[5])
{
for (int i = 1; i <= 3; i++)
{
sa[1][i] = le[i][3];
sb[1][i] = ri[4-i][1];
sc[1][i] = ba[3][4-i];
}
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
le[i][j] = sa[i][j];
ba[i][j] = sc[i][j];
ri[i][j] = sb[i][j];
}
return;
}
void change5(char (*le)[5], char (*ri)[5], char (*ba)[5])
{
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
sa[i][j] = le[i][j];
sb[i][j] = ri[i][j];
sc[i][j] = ba[i][j];
}
for (int i = 1; i <= 3; i++)
{
le[i][3] = sa[3][4-i];
ri[i][1] = sb[3][i];
ba[1][i] = sc[3][4-i];
}
return;
}
void dischange5(char (*le)[5], char (*ri)[5], char (*ba)[5])
{
for (int i = 1; i <= 3; i++)
{
sa[3][i] = le[4-i][3];
sb[3][i] = ri[i][1];
sc[3][i] = ba[1][4-i];
}
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
le[i][j] = sa[i][j];
ri[i][j] = sb[i][j];
ba[i][j] = sc[i][j];
}
return;
}
void clockwise(char (*fr)[5],char (*le)[5], char (*ri)[5], char (*to)[5], char (*bo)[5])
{
char s[10][10];
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
s[i][j] = fr[i][j];
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
fr[j][4-i] = s[i][j];
char x,y,z;
x = le[1][3];
y = le[2][3];
z = le[3][3];
for (int i = 1; i <= 3; i++)
le[i][3] = bo[1][i];
for (int i = 1; i <= 3; i++)
bo[1][i] = ri[4-i][1];
for (int i = 1; i <= 3; i++)
ri[i][1] = to[3][i];
to[3][1] = z;
to[3][2] = y;
to[3][3] = x;
return;
}
void counterclockwise(char (*fr)[5],char (*le)[5], char (*ri)[5], char (*to)[5], char (*bo)[5])
{
char s[10][10];
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
s[i][j] = fr[i][j];
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
fr[4-j][i] = s[i][j];
char x,y,z;
x = le[1][3];
y = le[2][3];
z = le[3][3];
for (int i = 1; i <= 3; i++)
le[i][3] = to[3][4-i];
for (int i = 1; i <= 3; i++)
to[3][i] = ri[i][1];
for (int i = 1; i <= 3; i++)
ri[i][1] = bo[1][4-i];
bo[1][1] = x;
bo[1][2] = y;
bo[1][3] = z;
return;
}
int main()
{
char fr[10][5],ba[10][5],le[10][5],ri[10][5],to[10][5],bo[10][5];
char map[100][100];
int n,m,t,k,x,y;
//freopen("yu.in","r",stdin);
//freopen("yu.out","w",stdout);
scanf("%d",&t);
k = 0;
while(t--)
{
for (int i = 0; i <= 9; i++)
cin.getline(map[i],100);
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
{
to[i][j] = map[i][2*j+4];
le[i][j] = map[3+i][2*j-2];
fr[i][j] = map[3+i][4+j*2];
ri[i][j] = map[3+i][10+2*j];
ba[i][j] = map[3+i][16+2*j];
bo[i][j] = map[6+i][4+j*2];
}
scanf("%d",&n);
for (int i = 1; i <= n; i++)
{
scanf("%d%d",&x,&y);
switch (x)
{
case 0 : change0(to,bo); if (y == 1) clockwise(le,ba,fr,to,bo); else
counterclockwise(le,ba,fr,to,bo); dischange0(to,bo); break;
case 1 : if (y == 1) clockwise(fr,le,ri,to,bo); else
counterclockwise(fr,le,ri,to,bo); break;
case 2 : change2(to,bo);if (y == 1) clockwise(ri,fr,ba,to,bo); else
counterclockwise(ri,fr,ba,to,bo); dischange2(to,bo); break;
case 3 : change3(to,bo);if (y == 1) clockwise(ba,ri,le,to,bo); else
counterclockwise(ba,ri,le,to,bo); dischange3(to,bo); break;
case 4 : change4(le,ri,ba);if (y == 1) clockwise(to,le,ri,ba,fr); else
counterclockwise(to,le,ri,ba,fr); dischange4(le,ri,ba); break;
case 5 : change5(le,ri,ba);if (y == 1) clockwise(bo,le,ri,fr,ba); else
counterclockwise(bo,le,ri,fr,ba); dischange5(le,ri,ba); break;
}
}
k++;
printf("Scenario #%d:\n",k);
for (int i = 1; i <= 3; i++)
printf(" %c %c %c\n",to[i][1],to[i][2],to[i][3]);
for (int i = 1; i <= 3; i++)
printf("%c %c %c %c %c %c %c %c %c %c %c %c\n",le[i][1],le[i][2],le[i][3],fr[i][1],fr[i][2],fr[i][3],ri[i][1],ri[i][2],ri[i][3],ba[i][1],ba[i][2],ba[i][3]);
for (int i = 1; i <= 3; i++)
printf(" %c %c %c\n",bo[i][1],bo[i][2],bo[i][3]);
printf("\n");
}
return 0;
}
相关文章推荐
- POJ 1955 Rubik's Cube
- POJ 1290 Grandpa's Rubik Cube 笔记
- Poj2618 Cube in Labyrinth (广搜 状态扩展)
- poj Cube Stacking(带权并查集)
- POJ-2155 Matrix 二维树状数组, HDU-3584 Cube 三维树状数组
- poj Cube Stacking(带权并查集)
- POJ-1988-Cube_Stacking-并查集
- POJ1988 Cube Stacking——并查集——Pku1988
- Cube Stacking POJ - 1988 并查集
- hdu 1541 Stars poj 1195 Mobile phones(二维) poj 2155 Matrix(二维) hdu 3584 Cube(三维) 树状数组
- 第四届 山东省ACM Rubik’s cube(爆搜)
- poj cube stacking
- POJ 1955 Rubik's Cube
- poj Cube Stacking(带权并查集)
- poj Cube Stacking(带权并查集)
- poj Cube Stacking(带权并查集)
- POJ 2155 Matrix ( 二维树状数组 ) || HDU 3584 Cube ( 三维树状数组 )
- poj Cube Stacking(带权并查集)
- Exploit writing tutorial part 10 : Chaining DEP with ROP – the Rubik’s[TM] Cube
- POJ 3547 Cube evolvent 英文少