USACO 1.2.2 Transformations 方块转换

该博客介绍了一个USACO题目,涉及N x N的方格图案通过90度、180度、270度旋转,水平反射以及组合变换后的识别。程序需确定最小的变换操作,当存在多种可能性时选择编号最小的变换。博客包含输入输出格式的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值