USACO 1.2 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


思路:模拟,枚举

/*
ID: shuyang1
PROG: transform
LANG: C++
*/
#include <cstdio>
#include <iostream>
#include <fstream>
using namespace std;

const int maxn=25;
char st[maxn][maxn];
char ed[maxn][maxn];
int ans=7;
int n;
void Init()
{
    freopen ("transform.in", "r",stdin);
    freopen ("transform.out", "w",stdout);
    int i,j;
    cin>>n;
    for(i=0;i<n;i++) cin>>st[i];
    for(i=0;i<n;i++) cin>>ed[i];
}

void cmp(int t)
{
    bool flag=true;
    int i,j;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
                if(st[i][j]!=ed[i][j]) flag=false;
    if(flag) ans=min(ans,t);
}

void Deg90()       //旋转90度,旋转180,270都可由他得到
{
    int t[maxn][maxn];
    int i,j;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            t[j][n-1-i]=st[i][j];      //t[j][n-i-1]=st[i][j]
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            st[i][j]=t[i][j];
}

void Ref()         // 反射  
{
    int t[maxn][maxn];
    int i,j;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            t[i][n-j-1]=st[i][j];   //t[j][n-j-1]=st[i][j]
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            st[i][j]=t[i][j];
}

int main()
{
    int i,j;
    Init();
    cmp(6); //先考虑是否不要操作,但是要是可以由比他小的操作实现还是选择比他小的操作。
    Deg90();
    cmp(1);
    Deg90();
    cmp(2);
    Deg90();
    cmp(3);
    Deg90();
    Ref();
    cmp(4);
    for(i=0;i<3;i++)
    {
        Deg90();
        cmp(5);
    }
    cout<<ans<<endl;
    return 0;
}

  

原文地址:https://www.cnblogs.com/shenshuyang/p/2491518.html