Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Determine Whether Matrix can Be Obtained By rotation
- #include <iostream>
- #include <vector>
- #include <iomanip>
- using namespace std;
- void Rotate(vector<vector<int>>& mat)
- {
- int n = mat.size();
- for (int i = 0; i < n; i++)
- {
- reverse(mat[i].begin(), mat[i].end());
- }
- for (int i = 0; i < n; i++) {
- for (int j = i + 1; j < n; j++) {
- swap(mat[i][j], mat[j][i]);
- }
- }
- }
- bool equals(vector<vector<int>>& mat, vector<vector<int>>& target) {
- int n = mat.size();
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if (mat[i][j] != target[i][j]) return false;
- }
- }
- return true;
- }
- bool findRotation(vector<vector<int>>& mat, vector<vector<int>>& target)
- {
- if (equals(mat, target)) return true;
- Rotate(mat);
- if (equals(mat, target)) return true;
- Rotate(mat);
- if (equals(mat, target)) return true;
- Rotate(mat);
- if (equals(mat, target)) return true;
- return false;
- }
- void printMatrix(vector<vector<int>>& mat)
- {
- int size = mat.size();
- for (int iRow = 0; iRow < size; iRow++)
- {
- for (int iCol = 0; iCol < size; iCol++)
- {
- cout << setw(3) << mat[iRow][iCol];
- }
- cout << endl;
- }
- }
- int main()
- {
- vector<vector<int>> testRotMatrix = {
- {0, 1, 2, 3},
- {4, 5, 6, 7},
- {8, 9, 10, 11},
- {12, 13, 14, 15},
- };
- printMatrix(testRotMatrix);
- cout << endl;
- Rotate(testRotMatrix);
- printMatrix(testRotMatrix);
- cout << endl;
- vector<vector<int>> mat = { {0,1},{1,2} };
- vector<vector<int>> target1 = { {1,0},{0,2} };
- vector<vector<int>> target2 = { {1, 0}, {2, 1} };
- cout << (findRotation(mat, target1) ? "true" : "false") << endl;
- cout << (findRotation(mat, target2) ? "true" : "false") << endl;
- }
Add Comment
Please, Sign In to add comment