USACO 1.2 Transformations

题目连接

http://train.usaco.org/usacoprob2?a=7LsqvWoYMNU&S=transform

Description

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.

Input

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

Output

A single line containing the number from 1 through 7 (described above) that categorizes the transformation required to change from the `before’ representation to the `after’ representation.

Sample Input

3

@-@

@@-
@-@
@—
—@

Sample Output

1

题意

给出原始矩阵和目标矩阵,一共有七种转换方式,给出最小可以达到目标矩阵的转换方式的编号。

题解

计算一下转化的公式,之后优雅地顺序执行判断即可,练习基本功的好题。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
ID: hyson601
PROG: transform
LANG: C++11
*/
#include <iostream>
using namespace std;
const int maxn = 100;
char m[maxn][maxn],t[maxn][maxn],a[maxn][maxn],now[maxn][maxn];
int n;
void cpy(){
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
t[i][j]=now[i][j];
}
void init(){
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
now[i][j]=m[i][j];
}
void rot90(){
cpy();
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
now[i][j]=t[n-1-j][i];
}
bool judge4(){
cpy();
for(int i=0;i<n;++i)
for(int j=0;j<n;++j){
now[i][j]=t[i][n-1-j];
}
}
bool judge5(){
char tt[maxn][maxn];
for(int i=0;i<n;++i)
for(int j=0;j<n;++j){
tt[i][j]=now[i][n-1-j];
}
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
if(tt[i][j]!=a[i][j])
return false;
return true;
}
bool check(){
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
if(now[i][j]!=a[i][j])
return false;
return true;
}
int fun(){
bool ok5=false;
init();
rot90();
if(check())
return 1;
if(judge5())
ok5=true;
rot90();
if(check())
return 2;
if(judge5())
ok5=true;
rot90();
if(check())
return 3;
if(judge5())
ok5=true;
init();
judge4();
if(check())
return 4;
if(ok5)
return 5;
init();
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
if(m[i][j]!=a[i][j])
return 7;
return 6;
}
int main(){
freopen("transform.in","r",stdin);
freopen("transform.out","w",stdout);
scanf("%d",&n);
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
cin>>m[i][j];
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
cin>>a[i][j];
printf("%d\n",fun());
return 0;
}