题目: 两个正方体是否等价。
写的代码有点丑…改一下再发一次吧。
#include <bits/stdc++.h>
using namespace std;
char s[20];
char a[10],b[10];
int check()
{
char rot[10];
for(int i = 0; i < 6; i++)
{
strcpy(rot,b);
rot[0] = b[i];
if(i == 0)
strcpy(rot,b);
else if(i == 1)
{
rot[1] = b[5];
rot[5] = b[4];
rot[4] = b[0];
}
else if( i == 2)
{
rot[1] = b[5];
rot[2] = b[4];
rot[3] = b[1];
rot[5] = b[3];
rot[4] = b[0];
}
else if( i == 3)
{
rot[1] = b[5];
rot[2] = b[1];
rot[3] = b[4];
rot[4] = b[0];
rot[5] = b[2];
}
else if( i == 4)
{
rot[1] = b[0];
rot[5] = b[1];
rot[4] = b[5];
}
else
{
rot[1] = b[4];
rot[5] = b[0];
rot[4] = b[1];
}
for(int j = 0; j < 4; j++)
{
if(strcmp(a, rot ) == 0)
return 1;
int temp = rot[1];
rot[1] = rot[2];
rot[2] = rot[4];
rot[4] = rot[3];
rot[3] = temp;
}
}
return 0;
}
int main()
{
while(~scanf("%s",s))
{
for(int i = 0; i < 6; i++)
a[i] = s[i];
a[6] = '\0';
for(int i = 6; i <= 12; i++)
b[i-6] = s[i];
if(check())
printf("TRUE\n");
else
printf("FALSE\n");
}
return 0;
}
改良版本.
#include <bits/stdc++.h>
using namespace std;
char s[20];
char a[10],b[10];
int rote[6][6] = {{0,1,2,3,4,5},{1,5,2,3,0,4},{2,5,4,1,0,3},{3,5,1,4,0,2},{4,0,2,3,5,1},{5,4,2,3,1,0}};
int check()
{
char rot[10];
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 6; j++)
rot[j] = b[rote[i][j]];
rot[6] = '\0';
for(int j = 0; j < 4; j++)
{
if(strcmp(a, rot ) == 0)
return 1;
int temp = rot[1];
rot[1] = rot[2];
rot[2] = rot[4];
rot[4] = rot[3];
rot[3] = temp;
}
}
return 0;
}
int main()
{
while(~scanf("%s",s))
{
for(int i = 0; i < 6; i++)
a[i] = s[i];
a[6] = '\0';
for(int i = 6; i <= 12; i++)
b[i-6] = s[i];
if(check())
printf("TRUE\n");
else
printf("FALSE\n");
}
return 0;
}