USACO--Transformations

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


我先说我的思路: 我就是先把图形进行题目要求中的操作进行旋转,如果条件满足,就输出操作数,并结束程序,结果 return 0用的好多啊=_=

下边是代码:

/*
ID:lk951201
PROB:transform
LANG:C++
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 105
#define pf printf
using namespace std;

int N;
char bef[MAXN][MAXN];   //bef数组用于存放最开始的图形
char mid[MAXN][MAXN];   //mid数组用于存放旋转的图形
char aft[MAXN][MAXN];   //aft数组存放最后的图形

bool is_equal()  //判断图形是不是相同
{
    bool ok=true;
    for(int i=0;i<N;i++)
    {
        if(strcmp(bef[i],aft[i]) ) {ok=false;break;}   //采用每一行比较的方式
    }
    return ok;
}

void copytobef()     //因为把顺时针旋转过的图形放在了mid数组中,在复制回来,以方便上边的判断图形是否相同
{
    for(int i=0;i<N;i++)
    {
        strcpy(bef[i],mid[i]); //和上边一样,一行一行的复制
    }
}

void trans_x()   //对图形进行X镜像翻转
{
    for(int i=0;i<N;i++)
    {
        int n=N-1;
        for(int j=0;j<=n;j++)
        {
            char t=bef[i][j];
            bef[i][j]=bef[i][n];
            bef[i][n]=t;
            n--;
        }
    }
}

void clockwize()     //对图形进行顺时针旋转,把旋转过的放到mid输注中
{
    int n=N-1;
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            mid[j][n]=bef[i][j];
        }
        n--;
    }
}

int main()
{
    freopen("transform.in","r",stdin);
    freopen("transform.out","w",stdout);
    memset(bef,0,sizeof(bef));     //数组进行清空
    memset(mid,0,sizeof(mid));
    memset(aft,0,sizeof(aft));
    scanf("%d",&N);
    for(int i=0;i<N;i++)
        scanf("%s",bef[i]);
    for(int i=0;i<N;i++)
        scanf("%s",aft[i]);

    clockwize();copytobef();   //第一次进行顺时针旋转,旋转过后在mid数组中,因为判断是在bef中,所以在复制到bef中
    if(is_equal()) {pf("1\n");return 0;} //如果图形一样,输出1,并结束

    clockwize();copytobef();   //第二次旋转
    if(is_equal()) {pf("2\n");return 0;}

    clockwize();copytobef();   //第三次旋转
    if(is_equal()) {pf("3\n");return 0;}

    clockwize();copytobef();trans_x();  //旋转四次,就恢复成原图形,这时候再进行X镜像旋转
    if(is_equal()) {pf("4\n");return 0;} //如果图形相同,~

    clockwize();copytobef();            //因为已经X镜像旋转过,所以依次旋转1次,判断
    if(is_equal()) {pf("5\n");return 0;}
    clockwize();copytobef();            //两次
    if(is_equal()) {pf("5\n");return 0;}
    clockwize();copytobef();            //三次
    if(is_equal()) {pf("5\n");return 0;}

    clockwize();copytobef();            //如果x旋转过后,在旋转1,2,3次后还不满足,再旋转一次,成最初的图形
    if(is_equal()) {pf("6\n");return 0;} //判断,为什么不开始就判断呢?因为如果都满足,先输出小的

    printf("7\n");return 0;  // 都不满足,只能输出7了
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值