USACO SEC.1.2 No.2 Transformations
题意:给出两个方阵,给出是否能够通过顺时针旋转或者对折使得方阵相同
解法:矩阵的旋转
写的不是很简洁
/* ID: lsswxr1 PROG: transform LANG: C++ */ #include <iostream> #include <vector> #include <map> #include <list> #include <set> #include <deque> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <cctype> #include <cstdio> #include <iomanip> #include <cmath> #include <cstdio> #include <string> #include <fstream> using namespace std; ///宏定义 const int INF = 1000000000; const int MAXN = 15; const int maxn = MAXN; ///全局变量 和 函数 #define USACO #ifdef USACO #define cin fin #define cout fout #endif ////////////////////////////////////////////////////////////////////////// int N; string src[maxn]; string tar[maxn]; string tmp[maxn], tmp2[maxn]; void rotat(string* rec, string* from, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { rec[j][n - i - 1] = from[i][j]; } } } void reflect(string* rec, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n / 2; j++) { char tempStr = rec[i][j]; rec[i][j] = rec[i][n - j - 1]; rec[i][n - j - 1] = tempStr; } } } bool recEq(string* rec1, string* rec2, int n) { for (int i = 0; i < n; i++) { if (rec1[i] != rec2[i]) return false; } return true; } int main() { #ifdef USACO ofstream fout ("transform.out"); ifstream fin ("transform.in"); #endif ///变量定义 while (cin >> N) { for (int i = 0; i < N; i++) { cin >> src[i]; tmp[i] = src[i]; } for (int i = 0; i < N; i++) { cin >> tar[i]; } // bool flag = true; rotat(tmp, src, N); if (recEq(tmp, tar, N)) { cout << 1 << endl; continue; } //2 flag = true; rotat(tmp, src, N); for (int i = 0; i < N; i++) tmp2[i] = tmp[i]; rotat(tmp, tmp2, N); if (recEq(tmp, tar, N)) { cout << 2 << endl; continue; } //3 flag = true; rotat(tmp, src, N); for (int i = 0; i < N; i++) tmp2[i] = tmp[i]; rotat(tmp, tmp2, N); for (int i = 0; i < N; i++) tmp2[i] = tmp[i]; rotat(tmp, tmp2, N); if (recEq(tmp, tar, N)) { cout << 3 << endl; continue; } //4 flag = true; for (int i = 0; i < N; i++) tmp[i] = src[i]; reflect(tmp, N); if (recEq(tmp, tar, N)) { cout << 4 << endl; continue; } //5 flag = true; for (int i = 0; i < N; i++) tmp[i] = src[i]; reflect(tmp, N); for (int i = 0; i < N; i++) tmp2[i] = tmp[i]; rotat(tmp, tmp2, N); if (recEq(tmp, tar, N)) { cout << 5 << endl; continue; } flag = true; for (int i = 0; i < N; i++) tmp[i] = src[i]; reflect(tmp, N); for (int i = 0; i < N; i++) tmp2[i] = tmp[i]; rotat(tmp, tmp2, N); for (int i = 0; i < N; i++) tmp2[i] = tmp[i]; rotat(tmp, tmp2, N); if (recEq(tmp, tar, N)) { cout << 5 << endl; continue; } flag = true; for (int i = 0; i < N; i++) tmp[i] = src[i]; reflect(tmp, N); for (int i = 0; i < N; i++) tmp2[i] = tmp[i]; rotat(tmp, tmp2, N); for (int i = 0; i < N; i++) tmp2[i] = tmp[i]; rotat(tmp, tmp2, N); for (int i = 0; i < N; i++) tmp2[i] = tmp[i]; rotat(tmp, tmp2, N); if (recEq(tmp, tar, N)) { cout << 5 << endl; continue; } //6 flag = true; for (int i = 0; i < N; i++) tmp[i] = src[i]; if (recEq(tmp, tar, N)) { cout << 6 << endl; continue; } //7 cout << 7 << endl; } ///结束 return 0; }