USACO Transformations 解题日志

一开始被这道题吓住了,以为还要考虑各种组合变换(而不仅仅是题目中所提到的那个“组合”),以为最后的输出可能有 “2  5”这种(随便举的。。。)

但是发现题目中说,取最小的数字。于是我明白,只用判断一下是不是1,2,3,4,5,6,如果都不是,输出7就好了。

考虑到N最大为10,及使用暴力,也不会花太多时间。

于是我采用了暴力做法。

对于旋转90°,180°和270°,我没有去想每个怎么写,直接每次在前一次的基础上调用一下转90°的就可以了。转4n次便回到了图形最开始的样子。

每次操作,我把中间产物middle复制到tmp中,这样操作写起来方便且不容易错(没有tmp时我写错了,嗯)。

最后的比较采用一个一个比的方式。

感觉自己写的代码比较长,不过思路还比较清晰。总感觉有些地方没考虑到,不过数据的全部过了的。

下面放上题目以及我的代码。


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

#include<iostream>
#include<fstream>
#include<cstdio>
#include<cstring>
using namespace std;

char begin[10][10];
char middle[10][10];
char end[10][10];
int N;

ifstream fin("transform.in");
ofstream fout("transform.out");

bool theSame()
{
	int num = 0;
	for (int i = 0; i < N; ++i)
		for (int j = 0; j < N; ++j){
			if (end[i][j] == middle[i][j])
				++num;
		}
	if (num == N * N) return 1;
	else return 0;	
}

void clockwise()
{
	char tmp[10][10];
	for (int i = 0; i < N; ++i)
		for (int j = 0; j < N; ++j)
			tmp[i][j] = middle[i][j];
			
	for (int i = 0; i < N; ++i)
		for (int j = 0; j < N; ++j){
			middle[j][N - i - 1] = tmp[i][j];
		}
}

void reflection()
{
	char tmp[10][10];
	for (int i = 0; i < N; ++i)
		for (int j = 0; j < N; ++j)
			tmp[i][j] = middle[i][j];
	
	for (int i = 0; i < N; ++i)
		for (int j = 0; j < N; ++j){
			middle[i][j] = tmp[i][N - j - 1];
		}
		 
}

void check()
{
	clockwise();
	if (theSame()){
		fout << "1" << endl;
		return;
	}
	
	clockwise();
	if (theSame()){
		fout << "2" << endl;
		return;
	}
	
	clockwise();
	if (theSame()){
		fout << "3" << endl;
		return;
	}
	
	clockwise();
	if (theSame()){
		fout << "6" << endl;
		return;
	}
	
	reflection(); 
	if (theSame()){
		fout << "4" << endl;
		return;
	}
	
	clockwise();
	if (theSame()){
		fout << "5" << endl;
		return;
	}
	
	clockwise();
	if (theSame()){
		fout << "5" << endl;
		return;
	}
	
	clockwise();
	if (theSame()){
		fout << "5" << endl;
		return;
	}
	
	fout << "7" << endl;
	
}

int main()
{
	fin >> N;
	for (int i = 0; i < N; ++i)
		for (int j = 0; j < N; ++j)
			fin >> begin[i][j];
			
	for (int i = 0; i < N; ++i)
		for (int j = 0; j < N; ++j)
			fin >> end[i][j];
			
	for (int i = 0; i < N; ++i)
		for (int j = 0; j < N; ++j)
			middle[i][j] = begin[i][j];
			
	check();
	
	fin.close();
	fout.close();
	
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值