USACO1.2.2 Transformations (方块转换)

来源:互联网 发布:看尚网络电视怎么样 编辑:程序博客网 时间:2024/05/29 04:46

A square pattern of size N x N (1 <= N <= 10) black and white square tiles is transformed into another square pattern. Write a program that will recognize the minimum transformation that has been applied to the original pattern given the following list of possible transformations:

  • #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.

In the case that more than one transform could have been used, choose the one with the minimum number above.

PROGRAM NAME: transform

INPUT FORMAT

Line 1: A single integer, NLine 2..N+1: N lines of N characters (each either `@' or `-'); this is the square before transformationLine N+2..2*N+1: N lines of N characters (each either `@' or `-'); this is the square after transformation

SAMPLE INPUT(file transform.in)

3@  -  @ -   -   -@ @ -@ -  @@ -   - -  -  @

OUTPUT FORMAT

A single line containing the the number from 1 through 7 (described above) that categorizes the transformation required to change from the `before' representation to the `after' representation.

SAMPLE OUTPUT (file transform.out)

1

题目大意:给出两个由字符组成的图形和7种变换规则,求两图形间变换方法。

解题思路:直接模拟,枚举每一种方案就行了,需要注意的是:USACO是不给用GOTO的,注意代码的清晰程度。

    设a是原始状态,b是改变后的状态,右旋90度:b[j][n-i+1]=a[i][j];水平翻转:b[i][n-j+1]:=a[i][j]。

我用的数组下标是从0开始的,所以与上面说的不一致,下面是我的代码:

/*USER:xingwen wangTASK:transformLANG:C*/#include<stdio.h>#include<string.h>void turn();  /*旋转90度*/ void find();  /*寻找匹配方案*/ int judge(int k); /*判断是否匹配*/ void fanshe();   /*水平翻转*/ void renew();   /*还原成原来的状态*/ int n;char origin[11][11],a[11][11],b[11][11],c[11][11];int main(){     freopen("transform.in","r",stdin);     freopen("transform.out","w",stdout);     int i;        scanf("%d",&n);     for(i=0;i<n;i++)     scanf("%s",origin[i]);     for(i=0;i<n;i++)     scanf("%s",b[i]);     renew();     find();     return 0;}void find(){     int i;     for(i=1;i<=3;i++)     {            turn();            if(judge(i))            return;     }     renew();     if(judge(6))     return;     fanshe();     if(judge(4))     return;     for(i=1;i<=3;i++)     {           turn();           if(judge(5))           return;     }     printf("7\n");}void renew(){     int i;     for(i=0;i<n;i++)     strcpy(a[i],origin[i]);}void turn(){     int i,j;     for(i=0;i<n;i++)     for(j=0;j<n;j++)     c[j][n-i-1]=a[i][j];/*数组下标从0开始*/      for(i=0;i<n;i++)     strcpy(a[i],c[i]);}int judge(int k){     int i;     for(i=0;i<n;i++)     if(strcmp(c[i],b[i])!=0)     return 0;     printf("%d\n",k);     return 1;}void fanshe(){     int i,j;     for(i=0;i<n;i++)     for(j=0;j<n;j++)     c[i][n-j-1]=a[i][j];     for(i=0;i<n;i++)     strcpy(a[i],c[i]);}