标签:
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:
In the case that more than one transform could have been used, choose the one with the minimum number above.
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 |
3 @-@ --- @@- @-@ @-- --@
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.
1
题解:模拟题
写一个旋转90度函数,和一个投影函数 其他几种情况可运用这两个函数实现
旋转90°:目标矩阵 after[j][N-i-1]=原矩阵 before[i][j]
投影: 目标矩阵 after[i][N-j-1]=原矩阵 before[i][j]
/* ID: cxq_xia1 PROG: transform LANG: C++ */ #include <iostream> #include <cstdio> using namespace std; const int maxn=11; int N; char before1[maxn][maxn],after1[maxn][maxn]; bool isSame(char before[maxn][maxn],char after[maxn][maxn]) { for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { if(before[i][j]!=after[i][j]) { return false; } } } return true; } void clockwise_90(char before[maxn][maxn],char after[maxn][maxn]) { for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { after[j][N-i-1]=before[i][j]; } } } void Reflection(char before[maxn][maxn],char after[maxn][maxn]) { for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { after[i][N-j-1]=before[i][j]; } } } int solve() { char wise_90[maxn][maxn],wise_180[maxn][maxn],wise_270[maxn][maxn],reflect[maxn][maxn]; clockwise_90(before1,wise_90); if(isSame(after1,wise_90)) return 1; clockwise_90(wise_90,wise_180); if(isSame(after1,wise_180)) return 2; clockwise_90(wise_180,wise_270); if(isSame(after1,wise_270)) return 3; Reflection(before1,reflect); if(isSame(after1,reflect)) return 4; Reflection(after1,reflect); if(isSame(wise_90,reflect)||isSame(wise_180,reflect)||isSame(wise_270,reflect)) return 5; if(isSame(before1,after1)) return 6; return 7; } int main() { freopen("transform.in","r",stdin); freopen("transform.out","w",stdout); while(cin >> N) { for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { cin >> before1[i][j]; } } for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { cin >> after1[i][j]; } } cout <<solve()<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/WillsCheng/p/4768356.html