Section 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

分析

本题首先给出了一个方阵(使用字符表示)的6种变换方式,分别包括旋转(90 180 270)、镜像反转、反转加旋转、保持原状,然后输入一个n*n的方阵以及变换后的结果,要求判断是由哪种变换得到的结果,如果不在上述变换种类中,则输出7.
要解决本题,首先要实现旋转和镜像反转两种基本的变换方式,这里只需要实现旋转90度以及镜像反转两种操作即可,其他操作都是这两种操作的组合。实现基本操作后只需要将原始状态和结果状态输入,依次判断七种变换方式即可。

提交代码

/*
ID: 
PROG: transform
LANG: C++
*/
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;

typedef vector<vector<char> > pattern;

pattern rotate90(pattern input,int n)
{
	pattern output;
	output.resize(n);
	for (int i=0;i!=n;i++)
	{
		output[i].resize(n);
	}
	for (int i=0;i!=n;i++)
	{
		for (int j=0;j!=n;j++)
		{
			output[i][j] = input[n-1-j][i];
		}
	}
	return output;
}

pattern reflect(pattern input,int n)
{
	pattern output;
	output.resize(n);
	for (int i=0;i!=n;i++)
	{
		output[i].resize(n);
	}
	for (int i=0;i!=n;i++)
	{
		for (int j=0;j!=n;j++)
		{
			output[i][j] = input[i][n-1-j];
		}
	}
	return output;
}

int transform(pattern source,pattern target,int n)
{


	if (target == rotate90(source,n))
	{
		return 1;
	}
	else if(target == rotate90(rotate90(source,n),n))
	{
		return 2;
	}
	else if (target == rotate90(rotate90(rotate90(source,n),n),n))
	{
		return 3;
	}
	else if (target == reflect(source,n))
	{
		return 4;
	}
	else if (target == source)
	{
		return 6;
	}
	else
	{
		if (target == rotate90(reflect(source,n),n)
			|| target == rotate90(rotate90(reflect(source,n),n),n)
			|| target == rotate90(rotate90(rotate90(source,n),n),n))
		{
			return 5;
		}
		else
			return 7;
	}
}

int main()
{
	ifstream fin("transform.in");
	int n;
	fin >> n;
	pattern t1(n);
	pattern t2(n);
	for (int i=0;i!=2*n;i++)
	{

		for (int j=0;j!=n;j++)
		{
			char a;
			fin >> a;
			if(i <n)
			{
				t1[i%n].push_back(a);
			}
			else
			{
				t2[i%n].push_back(a);
			}
		}
	}
	ofstream fout("transform.out");
	fout << transform(t1,t2,n) << endl;
	return 0;
}

提交结果

TASK: transform
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.008 secs, 3504 KB]
   Test 2: TEST OK [0.008 secs, 3504 KB]
   Test 3: TEST OK [0.008 secs, 3504 KB]
   Test 4: TEST OK [0.005 secs, 3504 KB]
   Test 5: TEST OK [0.008 secs, 3504 KB]
   Test 6: TEST OK [0.005 secs, 3504 KB]
   Test 7: TEST OK [0.003 secs, 3504 KB]
   Test 8: TEST OK [0.003 secs, 3504 KB]

All tests OK.

官方参考答案

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MAXN 10

typedef struct Board Board;
struct Board {
    int n;
    char b[MAXN][MAXN];
};

/* rotate 90 degree clockwise: [r, c] -> [c, n - r] */
Board
rotate(Board b)
{
    Board nb;
    int r, c;

    nb.n = b.n;
    for(r=0; r<b.n; r++)
        for(c=0; c<b.n; c++)
            nb.b[c][b.n-1 - r] = b.b[r][c];
    return nb;
}

/* reflect board horizontally: [r, c] -> [r, n-1 -c] */
Board
reflect(Board b)
{
    Board nb;
    int r, c;

    nb.n = b.n;
    for(r=0; r<b.n; r++)
        for(c=0; c<b.n; c++)
            nb.b[r][b.n-1 - c] = b.b[r][c];
    return nb;
}

/* return non-zero if and only if boards are equal */
int
eqboard(Board b, Board bb)
{
    int r, c;

    if(b.n != bb.n)
        return 0;
    for(r=0; r<b.n; r++)
        for(c=0; c<b.n; c++)
            if(b.b[r][c] != bb.b[r][c])
                return 0;
    return 1;
}

Board
rdboard(FILE *fin, int n)
{
    Board b;
    int r, c;

    b.n = n;
    for(r=0; r<n; r++) {
        for(c=0; c<n; c++)
            b.b[r][c] = getc(fin);
        assert(getc(fin) == '\n');
    }
    return b;
}

void
main(void)
{
    FILE *fin, *fout;
    Board b, nb;
    int n, change;

    fin = fopen("transform.in", "r");
    fout = fopen("transform.out", "w");
    assert(fin != NULL && fout != NULL);

    fscanf(fin, "%d\n", &n);
    b = rdboard(fin, n);
    nb = rdboard(fin, n);

    if(eqboard(nb, rotate(b)))
        change = 1;
    else if(eqboard(nb, rotate(rotate(b))))
        change = 2;
    else if(eqboard(nb, rotate(rotate(rotate(b)))))
        change = 3;
    else if(eqboard(nb, reflect(b)))
        change = 4;
    else if(eqboard(nb, rotate(reflect(b)))
         || eqboard(nb, rotate(rotate(reflect(b))))
         || eqboard(nb, rotate(rotate(rotate(reflect(b))))))
        change = 5;
    else if(eqboard(nb, b))
        change = 6;
    else
        change = 7;

    fprintf(fout, "%d\n", change);

    exit(0);
}

THE END


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值