【几何翻转】Transformations 方块转换(Usaco_Training 1.2)

Transformations

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, N
Line 2..N+1:N lines of N characters (each either `@' or `-'); this is the square before transformation
Line 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

翻译

 

 

初看此题说实话头都大了,很纠结。。。

但还是要理清楚集合坐标,1,2,3都可以归结为1,所以这里只说一下旋转90°

坐标(x,y)顺时针旋转90°之后(注意!!是之后的坐标)为(y,n-x-1)
还有要特别说明,这是在0基准的基础上变换的,1基准又会不一样

剩下的180°  270°只需再转就行了

刚才已经说了,(y,n-x-1)是旋转之后的坐标,也就是如果a是起始状态,c是旋转后的,就应该成 c[y][n-x-1]=a[x][y]  而不是 c[x][y]=a[y][n-x-1]  (仔细想想,后面这种就成了逆时针了,如果理解吃力,可以画图看看)

还有题目说了,要选序号最小的一个,所以即使初始和目标相同,如果1-5中能完成,就要输出1-5中的编号,如果1-5都不能完成了,才能输出6

剩下的就只有仔细了

C++ Code

/*
ID: jiangzh15
TASK: transform
LANG: C++
http://blog.csdn.net/jiangzh7
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

char a[20][20],b[20][20],c[20][20];
int n;

bool check(char b[][20],char c[][20])
{
    bool flag=true;
    for(int i=0;i<n;i++)
        if(memcmp(b[i],c[i],n))
        {
            flag=false;
            break;
        }
    return flag;
}

void work(int k)
{
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            switch(k)
            {
                case 1:c[j][n-i-1]=a[i][j];break;
                case 2:c[n-i-1][n-j-1]=a[i][j];break;
                case 3:c[n-j-1][i]=a[i][j];break;
                case 4:c[i][n-j-1]=a[i][j];break;
            }
}

int main()
{
    freopen("transform.in","r",stdin);
    freopen("transform.out","w",stdout);
    scanf("%d\n",&n);
    int i,j;
    for(i=0;i<n;i++) scanf("%s\n",a[i]);
    for(i=0;i<n;i++) scanf("%s\n",b[i]);
    bool same=check(a,b);
    //printf("\na:\n");for(i=0;i<n;i++)puts(a[i]);
    //printf("\nb:\n");for(i=0;i<n;i++)puts(b[i]);
    for(i=1;i<=4;i++)
    {
        work(i);
        //printf("\ni=%d\n",i);for(j=0;j<n;j++)puts(c[j]);
        if(check(b,c)){printf("%d\n",i);exit(0);}
    }
    //for(i=0;i<n;i++)puts(b[i]);
    work(4);//执行方案5  先翻转
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            a[i][j]=c[i][j];
   // printf("\n");for(i=0;i<n;i++)puts(a[i]);
    for(i=1;i<=3;i++)
    {
        work(i);
       // printf("\ni=%d\n",i);for(j=0;j<n;j++)puts(c[j]);
        if(check(b,c)) {printf("5\n");exit(0);}
    }
    if(same){printf("6\n");exit(0);}
    else {printf("7\n");exit(0);}
    return 0;
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值