POJ 1955 -- Rubik's Cube

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;
 } 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值