您的位置:首页 > 编程语言 > C语言/C++

USACO Transformations 解题日志

2016-02-14 21:34 441 查看
一开始被这道题吓住了,以为还要考虑各种组合变换(而不仅仅是题目中所提到的那个“组合”),以为最后的输出可能有 “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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  USACO C++ 水题