P1205 方塊變換題解
阿新 • • 發佈:2021-07-21
#include <bits/stdc++.h> using namespace std; const int N = 15; char a[N][N]; char c[N][N]; char t[N][N]; char t2[N][N]; int n; //判斷兩個二維陣列是不是一致 bool equal() { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (t[i][j] != c[i][j]) return false; return true; } int main() { cin >> n; //讀入原始圖案 for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) cin >> a[i][j]; //讀入目標圖案 for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) cin >> c[i][j]; //1、順時針轉90度 //通過實際例子,找規律,得i,i--->j,n-i+1 /** 1 2 3 7 4 1 4 5 6 --> 8 5 2 7 8 9 9 6 3 以數字1為例,原來的座標(1,1),現在的座標(1,3) 以數字2為例,原來的座標(1,2),現在的座標(2,3) 以數字3為例,原來的座標(1,3),現在的座標(3,3) 成功發現行座標變成了列座標。那麼列座標呢?它是怎麼變化的呢? 以7,8,9為例,它們原來在不同的列,現在都是第1列,看來後期的 列與行有關,當然也得與n有關,所以猜測試為n-i,結果呢,3-3=0,不對啊, 再加個1試試,就是n-i+1,再驗算一下,完全正確! */ for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) t[j][n - i + 1] = a[i][j]; //如果通過,輸出1,返回 if (equal()) { cout << 1 << endl; return 0; } //2、順時針轉180度 //通過實際例子,找規律,得x,y---> n-x+1,n-y+1 /** 1 2 3 9 8 7 4 5 6 --> 6 5 4 7 8 9 3 2 1 用紙畫出來模擬一下 找規律 1,1--->n,n 1,2--->n,n-1 1,3--->n,n-2 2,1--->n-1,n 2,2--->n-1,n-1 總結 : x,y---> n-x+1,n-y+1 */ for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) t[n - i + 1][n - j + 1] = a[i][j]; //如果通過,輸出2,返回 if (equal()) { cout << 2 << endl; return 0; } //3、順時針轉270度 //通過實際例子,找規律,得x,y---> n-y+1,x /** 1 2 3 3 6 9 4 5 6 --> 2 5 8 7 8 9 1 4 7 用紙畫出來模擬一下 找規律 1,1--->n,1 1,2--->n-1,1 1,3--->n-2,1 2,1--->n,2 2,2--->n-1,2 總結 : x,y---> n-y+1,x */ for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) t[n - j + 1][i] = a[i][j]; //如果通過,輸出3,返回 if (equal()) { cout << 3 << endl; return 0; } //4、圖案在水平方向翻轉 //通過實際例子,找規律,得x,y---> x,n-y+1 /** 1 2 3 3 2 1 4 5 6 --> 6 5 4 7 8 9 9 8 7 用紙畫出來模擬一下 找規律 1,1--->1,n 1,2--->1,n-1 1,3--->1,n-2 2,1--->2,n 2,2--->2,n-1 總結 : x,y---> x,n-y+1 */ for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) t[i][n - j + 1] = a[i][j]; //如果通過,輸出4,返回 if (equal()) { cout << 4 << endl; return 0; } //5、組合 圖案在水平方向翻轉, // 然後再按照 1 ∼3 之間的一種再次轉換。 //*********************************************************************************************** //5.1 水平+90度 for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) t[i][n - j + 1] = a[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) t2[i][j] = t[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) t[j][n - i + 1] = t2[i][j]; //如果通過,輸出5,返回 if (equal()) { cout << 5 << endl; return 0; } //*********************************************************************************************** //*********************************************************************************************** //5.2 水平+180度 for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) t[i][n - j + 1] = a[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) t2[i][j] = t[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) t[n - i + 1][n - j + 1] = t2[i][j]; //如果通過,輸出5,返回 if (equal()) { cout << 5 << endl; return 0; } //*********************************************************************************************** //*********************************************************************************************** //5.3 水平+270度 for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) t[i][n - j + 1] = a[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) t2[i][j] = t[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) t[n - j + 1][i] = t2[i][j]; //如果通過,輸出5,返回 if (equal()) { cout << 5 << endl; return 0; } //*********************************************************************************************** //6、不改變 for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) t[i][j] = a[i][j]; //如果通過,輸出6,返回 if (equal()) { cout << 6 << endl; return 0; } //7、無效轉換 cout << 7 << endl; return 0; }