USACO 1.2.2 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 【分析】 這道題其實就是對數組的變換,雖說有7種變換方式,其實只有兩種,一個是順時針旋轉90度,另一個是左右對稱, 其他的都可以由這兩個變換出來。 一開始先寫出三個函數,一個是把數組順時針旋轉90度的函數(在下面成為trans函數),另一個是左右對稱的函數(在 下面稱為fanshe函數),最後一個是判斷當前狀態與目標狀態是否相等的函數(check)。 【變換方式】 第一種 旋轉90度:調用一次trans(),然後check(); 第二種 旋轉180度:兩次調用trans(),然後check(); 第三種 旋轉270度:三次調用trans(),然後check(); 第四種 反射:直接調用fanshe(),然後check(); 第五種 組合: 1)先調用fanshe(),然後執行第一種變換方式,然後check(); 2)先調用fanshe(),然後執行第二種變換方式,然後check(); 3)先調用fanshe(),然後執行第三種變換方式,然後check(); 第六種 無變化:直接調用check() 第七種 無法變換:最後在都無法變換的情況下,輸出7即可。 按照慣例,貼上我的複雜、冗長的代碼:
/* ID:zyfwork1 PROB:transform LANG:C++ */ #include <iostream> #include <fstream> #include <cstdio> #include <cstring> using namespace std; ifstream fin("transform.in"); ofstream fout("transform.out"); char tran[12][12]; char res[12][12]; char temp[12][12]; char now[12][12]; int l; //正方形的邊長 void init(); void print(); void trans(); void init() { fin>>l; register unsigned short int i,j; for (i=1;i<=l;i++) for (j=1;j<=l;j++) fin>>tran[i][j]; for (i=1;i<=l;i++) for (j=1;j<=l;j++) fin>>res[i][j]; return; } void print(int num) { for (int i=1;i<=l;i++) { for (int j=1;j<=l;j++) { if (num==1) fout<<tran[i][j]; if (num==2) fout<<temp[i][j]; if (num==3) fout<<res[i][j]; if (num==4) fout<<now[i][j]; } fout<<endl; } } void trans() { register unsigned short int i,j; for (i=1;i<=l;i++) { int t=1; for (j=l;j>=1;j--) { temp[i][t]=now[j][i]; t++; } } for (i=1;i<=l;i++) for (j=1;j<=l;j++) now[i][j]=temp[i][j]; } void initnow() { register unsigned short int i,j; for (i=1;i<=l;i++) for (j=1;j<=l;j++) now[i][j]=tran[i][j]; } void circle(int num) { for (int i=1;i<=num;i++) trans(); } bool check() { register unsigned short int i,j; for (i=1;i<=l;i++) { for (j=1;j<=l;j++) { if(now[i][j]!=res[i][j]) return false; } } return true; } void fanshe() { register unsigned short int i,j; for (i=1;i<=l;i++) { for (j=1;j<=l;j++) { now[i][j]=tran[i][l-j+1]; } } } int main() { init(); initnow(); circle(1); if (check()) { fout<<1<<endl; return 0; } initnow(); circle(2); if (check()) { fout<<2<<endl; return 0; } initnow(); circle(3); if (check()) { fout<<3<<endl; return 0; } fanshe(); if (check()) { fout<<4<<endl; return 0; } initnow(); fanshe(); circle(1); if (check()) {fout<<5<<endl; return 0;} fanshe(); circle(2); if (check()) {fout<<5<<endl; return 0;} fanshe(); circle(3); if (check()) {fout<<5<<endl; return 0;} initnow(); if (check()) { fout<<6<<endl; return 0; } fout<<7<<endl; return 0; }
2020年2月16日 00:53
The program is fit for the automatic means for the futurities. The improvement of the goals and boom essays reviews are done for future items. The suffering for all ideal paths for the field. The thing is inducted with the help of the norms. The fitness is held for the culmination of the norms and all classes for future items. the argument is fit for the options for the team and elevation in all norms.