[cpp] /********************************* * 日期:2013-1-29 * 作者:SJF0115 * 題號: 九度1164 * 題目:旋轉矩陣 * 來源:http://ac.jobdu.com/problem.php?pid=1164 * 結果:AC * 題意: * 總結: **********************************/ #include<stdio.h> #include<stdlib.h> #include<string.h> //旋轉90° int Rotate(int Matrix[9][9],int n){ int i,j; int RotateMatrix[9][9]; for(i = 0;i < n;i++){ for(j = 0;j < n;j++){ RotateMatrix[j][n-1-i] = Matrix[i][j]; } } for(i = 0;i < n;i++){ for(j = 0;j < n;j++){ Matrix[i][j] = RotateMatrix[i][j]; } } return 0; } //判斷是否是旋轉90° int Judge(int Matrix[9][9],int Matrix2[9][9],int n){ int flag = 0,i,j; for(i = 0;i < n;i++){ for(j = 0;j < n;j++){ if(Matrix[i][j] != Matrix2[i][j]){ flag = 1; break; } } } //判斷失敗 if(flag == 1){ return 0; } else{ return 1; } } int main() { int n,i,j; freopen("C:\\Users\\SJF\\Desktop\\acm.txt","r",stdin); while(scanf("%d",&n)!=EOF) { int Matrix[9][9],Matrix2[9][9]; //輸入數據 for(i = 0;i < n;i++){ for(j = 0;j < n;j++){ scanf("%d",&Matrix[i][j]); } } for(i = 0;i < n;i++){ for(j = 0;j < n;j++){ scanf("%d",&Matrix2[i][j]); } } //判斷 if(Judge(Matrix,Matrix2,n) == 1){ printf("0\n"); } else{ Rotate(Matrix,n); if(Judge(Matrix,Matrix2,n) == 1){ printf("90\n"); } else{ Rotate(Matrix,n); if(Judge(Matrix,Matrix2,n) == 1){ printf("180\n"); } else{ Rotate(Matrix,n); if(Judge(Matrix,Matrix2,n) == 1){ printf("270\n"); } else{ printf("-1\n"); } } } } } return 0; } /********************************* * 日期:2013-1-29 * 作者:SJF0115 * 題號: 九度1164 * 題目:旋轉矩陣 * 來源:http://ac.jobdu.com/problem.php?pid=1164 * 結果:AC * 題意: * 總結: **********************************/ #include<stdio.h> #include<stdlib.h> #include<string.h> //旋轉90° int Rotate(int Matrix[9][9],int n){ int i,j; int RotateMatrix[9][9]; for(i = 0;i < n;i++){ for(j = 0;j < n;j++){ RotateMatrix[j][n-1-i] = Matrix[i][j]; } } for(i = 0;i < n;i++){ for(j = 0;j < n;j++){ Matrix[i][j] = RotateMatrix[i][j]; } } return 0; } //判斷是否是旋轉90° int Judge(int Matrix[9][9],int Matrix2[9][9],int n){ int flag = 0,i,j; for(i = 0;i < n;i++){ for(j = 0;j < n;j++){ if(Matrix[i][j] != Matrix2[i][j]){ flag = 1; break; } } } //判斷失敗 if(flag == 1){ return 0; } else{ return 1; } } int main() { int n,i,j; freopen("C:\\Users\\SJF\\Desktop\\acm.txt","r",stdin); while(scanf("%d",&n)!=EOF) { int Matrix[9][9],Matrix2[9][9]; //輸入數據 for(i = 0;i < n;i++){ for(j = 0;j < n;j++){ scanf("%d",&Matrix[i][j]); } } for(i = 0;i < n;i++){ for(j = 0;j < n;j++){ scanf("%d",&Matrix2[i][j]); } } //判斷 if(Judge(Matrix,Matrix2,n) == 1){ printf("0\n"); } else{ Rotate(Matrix,n); if(Judge(Matrix,Matrix2,n) == 1){ printf("90\n"); } else{ Rotate(Matrix,n); if(Judge(Matrix,Matrix2,n) == 1){ printf("180\n"); } else{ Rotate(Matrix,n); if(Judge(Matrix,Matrix2,n) == 1){ printf("270\n"); } else{ printf("-1\n"); } } } } } return 0; }