码迷,mamicode.com
首页 > 其他好文 > 详细

USACO 1.2 Transformations

时间:2015-08-29 09:42:10      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

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

题解:模拟题

写一个旋转90度函数,和一个投影函数 其他几种情况可运用这两个函数实现

旋转90°:目标矩阵 after[j][N-i-1]=原矩阵 before[i][j] 

投影:  目标矩阵 after[i][N-j-1]=原矩阵 before[i][j]

 

/*
ID: cxq_xia1
PROG: transform
LANG: C++
*/

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=11;
int N;
char before1[maxn][maxn],after1[maxn][maxn];

bool isSame(char before[maxn][maxn],char after[maxn][maxn])
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            if(before[i][j]!=after[i][j])
            {
                return false;
            }
        }
    }
    return true;
}


void clockwise_90(char before[maxn][maxn],char after[maxn][maxn])
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            after[j][N-i-1]=before[i][j];
        }
    }
}

void Reflection(char before[maxn][maxn],char after[maxn][maxn])
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            after[i][N-j-1]=before[i][j];
        }
    }
}

int solve()
{
    char wise_90[maxn][maxn],wise_180[maxn][maxn],wise_270[maxn][maxn],reflect[maxn][maxn];

    clockwise_90(before1,wise_90);
    if(isSame(after1,wise_90))
        return 1;

    clockwise_90(wise_90,wise_180);
    if(isSame(after1,wise_180))
        return 2;

    clockwise_90(wise_180,wise_270);
    if(isSame(after1,wise_270))
        return 3;

    Reflection(before1,reflect);
    if(isSame(after1,reflect))
        return 4;

    Reflection(after1,reflect);
    if(isSame(wise_90,reflect)||isSame(wise_180,reflect)||isSame(wise_270,reflect))
        return 5;

    if(isSame(before1,after1))
        return 6;

    return 7;

}

int main()
{
    freopen("transform.in","r",stdin);
    freopen("transform.out","w",stdout);
    while(cin >> N)
    {
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<N;j++)
            {
                cin >> before1[i][j];
            }
        }

        for(int i=0;i<N;i++)
        {
            for(int j=0;j<N;j++)
            {
                cin >> after1[i][j];
            }
        }
        cout <<solve()<<endl;
    }
    return 0;
}

  

USACO 1.2 Transformations

标签:

原文地址:http://www.cnblogs.com/WillsCheng/p/4768356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!