USACO 1.2.2 Transformations 方块转换

Transformations

A square pattern of size N x N (1 <= N <= 10) black and whitesquare tiles is transformed into another square pattern. Write a programthat will recognize the minimum transformation that has been applied tothe 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 (turnedinto a mirror image of itself by reflecting around a vertical line in themiddle of the image).
  • #5: Combination: The pattern was reflected horizontally and thensubjected 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 beforetransformation
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 (describedabove) that categorizes the transformation required to change from the`before' representation to the `after' representation.

SAMPLE OUTPUT (file transform.out)

1

解题思路:
        该题同样是模拟题,模拟的是坐标转换问题,主要包括顺时针旋转90度、顺时针旋转180度、顺时针旋转270度(即逆时针旋转90度)、镜面映射、映射后再旋转,等
过程,其实质是坐标关系问题,所以无论求解那个过程都要找到所对应的关系,然后接下来的问题,就迎刃而解了,具体实现见代码。
代码如下:
/*
ID:ayludon3
LANG: C++
TASK: transform
*/

#include <iostream>
#include <fstream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;

char pic1[10][10],pic2[10][10];
int n;
int work()
{
    int i,j,k=7;
    char trans1[10][10],trans2[10][10],trans3[10][10],trans4[10][10],trans5[10][10],trans6[10][10],trans7[10][10];
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
        {
            trans1[i][j]=pic1[n-j-1][i];
            trans2[i][j]=pic1[n-i-1][n-j-1];
            trans3[i][j]=pic1[j][n-i-1];
            trans4[i][j]=pic1[i][n-j-1];
            trans6[i][j]=pic1[n-j-1][n-i-1];
            trans7[i][j]=pic1[j][i];
        }
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            trans5[i][j]=trans4[n-i-1][n-j-1];
    int judge=0;
    for(i=0;i<n;i++)
    {    for(j=0;j<n;j++)
        {
            if(pic2[i][j]==trans1[i][j])
                k=1;
            else
            {
//                cout<<"跳出循环1!";
                k=7;
                judge=1;
                break;
            }
        }
        if(judge)
         break;
    }
//    cout<<"K1: "<<k<<endl;
    if(k==1)
        return 1;
    judge=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            //cout<<trans2[i][j]<<' ';
            if(pic2[i][j]==trans2[i][j])
                k=2;
            else
            {
                k=7;
                judge=1;
//                cout<<"跳出循环2!";
                break;
            }
        }
        if(judge)
            break;
    }
//    cout<<"K2: "<<k<<endl;
    if(k==2)
        return 2;
    judge=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(pic2[i][j]==trans3[i][j])
                k=3;
            else
            {
                k=7;
                judge=1;
                break;
            }
        }
        if(judge)
                break;
    }
    if(k==3)
        return 3;
    judge=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(pic2[i][j]==trans4[i][j])
                k=4;
            else
            {
                k=7;
                judge=1;
                break;
            }
        }
        if(judge)
            break;
    }
    if(k==4)
        return 4;
    judge=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(pic2[i][j]==trans6[i][j])
                k=5;
            else
            {
                k=7;
                judge=1;
                break;
            }
        }
        if(judge)
            break;
    }
    if(k==5)
        return 5;
    judge=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(pic2[i][j]==trans7[i][j])
                k=5;
            else
            {
                k=7;
                judge=1;
                break;
            }
        }
        if(judge)
            break;
    }
    if(k==5)
        return 5;
    judge=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(pic2[i][j]==trans5[i][j])
                k=5;
            else
            {
                k=7;
                judge=1;
                break;
            }
        }
        if(judge)
            break;
    }
    if(k==5)
        return 5;
    judge=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(pic2[i][j]==pic1[i][j])
                k=6;
            else
            {
                k=7;
                judge=1;
                break;
            }
        }
        if(judge)
            break;
    }
    if(k==6)
        return 6;
    return k;
}

int main()
{
//    ifstream fin ("transform.in");
//    ofstream fout ("transform.out");
    int i,j;
//    fin>>n;
    cin>>n;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
//            fin>>pic1[i][j];
            cin>>pic1[i][j];
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
//            fin>>pic2[i][j];
            cin>>pic2[i][j];
    //work();
//    fout<<work()<<endl;
    cout<<work();
    return 0;
}




你可能感兴趣的:(USACO 1.2.2 Transformations 方块转换)