现在的位置: 首页 > 综合 > 正文

Transformations

2013年09月06日 ⁄ 综合 ⁄ 共 3004字 ⁄ 字号 评论关闭

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



建议行列都从1开始计数阿阿阿阿阿

/*
ID: des_jas1
PROG: transform
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
//#define fin cin   注意字符串传入列从0开始,行从1开始,string不能在某个i行j列赋值
//#define fout cout
using namespace std;

const int MAXN=10+5;
char before[MAXN][MAXN],middle[MAXN][MAXN],after[MAXN][MAXN];
int N;

bool IS_90degree(char tp[MAXN][MAXN])
{
	int i,j,r;
	for(i=1,r=N-1;i<=N;i++,r--)
	{
		for(j=0;j<N;j++)
			if(tp[i][j]!=after[j+1][r])
				return false;
	}
	return true;
}

bool IS_180degree(char tp[MAXN][MAXN])
{
	int i,j,r,k;
	for(i=1,r=N+1-i;i<=N;i++,r--)
	{
		for(j=0,k=N-1;j<N;j++,k--)
			if(tp[i][j]!=after[r][k])
				return false;
	}
	return true;
}

bool IS_270degree(char tp[MAXN][MAXN])
{
	int i,j,r;
	for(i=1;i<=N;i++)
	{
		for(j=0,r=N;j<N;j++,r--)
			if(tp[i][j]!=after[r][i-1])
				return false;
	}
	return true;
}

bool IS_Reflection(char tp[MAXN][MAXN])
{
	int i,j,r;
	for(i=1;i<=N;i++)
	{
		for(j=0,r=N-1;j<N;j++,r--)
			if(tp[i][j]!=after[i][r])
				return false;
	}
	return true;
}

void reflection()
{
	int i,j,r;
	for(i=1;i<=N;i++)
	{
		for(j=0,r=N-1;j<N;j++,r--)
			middle[i][j]=before[i][r];
	}
}

bool IS_Combination()
{
	reflection();
	if(IS_90degree(middle))
		return true;
	if(IS_180degree(middle))
		return true;
	if(IS_270degree(middle))
		return true;
	if(IS_Reflection(middle))
		return true;
	return false;
}

int main() 
{
	ofstream fout ("transform.out");
    ifstream fin ("transform.in");
	int i,flag=0;
	fin>>N;
	for(i=1;i<=N;i++)
		fin>>before[i];
	for(i=1;i<=N;i++)
	{
		fin>>after[i];
		if(strcmp(after[i],before[i]))
			flag=1;         //判断是否和原来的一样
	}
    if(IS_90degree(before))
		fout<<"1";
	else if(IS_180degree(before)) //都用else if 而不用if意思就是有一个可以了其他就不用判断了
		fout<<"2";
	else if(IS_270degree(before))
		fout<<"3";
	else if(IS_Reflection(before))
		fout<<"4";
	else if(IS_Combination())
		fout<<"5";
	else if(!flag)
		fout<<"6";
	else
		fout<<"7";
	fout<<endl;
	fout.close();
	fin.close();
    return 0;
}

抱歉!评论已关闭.