[usaco]Transformations题解

题目:

输入一个数字n表示n阶方阵,然后输入两个n阶方阵(就俩符号@和-)分别表示起始状态和结束状态。共有7种操作,如下所示
  • #1: 90 Degree Rotation: The pattern was rotated clockwise 90 degrees.
  • #2: 180 Degree Rotation: The pattern was rotated clockwise 180 degrees.
  • #3: 270 Degree Rotation: The pattern was rotated clockwise 270 degrees.
  • #4: Reflection: The pattern was reflected horizontally (turned into a mirror image of itself by reflecting around a vertical line in the middle of the image).
  • #5: Combination: The pattern was reflected horizontally and then subjected to one of the rotations (#1-#3).
  • #6: No Change: The original pattern was not changed.
  • #7: Invalid Transformation: The new pattern was not obtained by any of the above methods.
最后输出一个数字即可。

题解:

首先把操作翻译成代码,然后不能上来就把6判断了,因为有的是旋转和他本身一样的。中间我曾一度以为翻转是可以左右也可以上下的,后来测试发现并不行……最后测试的结果就是没有左右翻转这种操作。

个人感觉我对题目还是有点理解不清,但是根据他给的测试用例你就能明白他到底在做什么。

代码:


/*
ID: xcwhkh1
LANG: C
TASK: transform
*/
#include
#include


void op1(char (*b)[11],int n)
{
char temp[11][11];
for(int i=0;i for(int j=0;j temp[i][j]=b[n-1-j][i];
for(int i=0;i for(int j=0;j b[i][j]=temp[i][j];
}
void op2(char (*b)[11],int n)
{
op1(b,n);
op1(b,n);
}
void op3(char (*b)[11],int n)
{
op1(b,n);
op1(b,n);
op1(b,n);
}
void op4(char (*b)[11],int n)
{
char temp[11][11];
for(int i=0;i for(int j=0;j temp[i][j]=b[i][n-1-j];
for(int i=0;i for(int j=0;j b[i][j]=temp[i][j];
}


void copy(char (*x)[11],char (*y)[11])
{
for(int i=0;i<11;i++)
strcpy(x[i],y[i]);
}
int cmp(char (*x)[11],char (*y)[11])
{
for(int i=0;i<11;i++)
if(strcmp(x[i],y[i])!=0)
return 0;
return 1;
}
int main () {
FILE *fin  = fopen ("transform.in", "r");
    FILE *fout = fopen ("transform.out", "w");
char a[11][11];
char b[11][11];
memset(a,0,121);
memset(b,0,121);
char cp[11][11];
memset(cp,0,121);
int n;
fscanf(fin,"%d",&n);
for(int i=0;i fscanf(fin,"%s",a[i]);
for(int i=0;i fscanf(fin,"%s",b[i]);


copy(cp,a);
op1(cp,n);
if(cmp(cp,b))
{fprintf(fout,"1\n");return 0;}

copy(cp,a);
op2(cp,n);
if(cmp(cp,b))
{fprintf(fout,"2\n");return 0;}

copy(cp,a);
op3(cp,n);
if(cmp(cp,b))
{fprintf(fout,"3\n");return 0;}

copy(cp,a);
op4(cp,n);
if(cmp(cp,b))
{fprintf(fout,"4\n");return 0;}

op4(a,n);//进行一次翻转后重复123操作,只不过都返回5

copy(cp,a);
op1(cp,n);
if(cmp(cp,b))
{fprintf(fout,"5\n");return 0;}

copy(cp,a);
op2(cp,n);
if(cmp(cp,b))
{fprintf(fout,"5\n");return 0;}

copy(cp,a);
op3(cp,n);
if(cmp(cp,b))
{fprintf(fout,"5\n");return 0;}


op4(a,n);//记得要翻转回来


if(cmp(a,b))
{fprintf(fout,"6\n");return 0;}
fprintf(fout,"7\n");



    return 0;
}

你可能感兴趣的:(未修改版)