检查矩阵 T 是否是矩阵垫旋转一次或多次 90° 的结果
给定两个大小分别为M×N和P×Q 的二维矩阵 mat[][]和T[][]。任务是检查矩阵T[][]是否是矩阵mat[][]旋转一个或多个90°的结果。
例子:
Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, T[][] ={{7, 4, 1}, {8, 5, 2}, {9, 6, 3}}
Output: Yes
Explanation:
From the figures given above, it is clear that T[][] is obtained by rotating 90° once.
Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, T[][] = {{1, 4, 7}, {8, 5, 2}, {9, 6, 3}}
Output: No
方法:根据以下观察可以解决给定的问题:
- Consider the following 2D matrix:
- Rotating it several times by 90° gets the following matrices:
- From the above figures, it can be seen that every row can either occur as it is or in its reversed form.
- The same can be observed for the columns
Hence, if T[][] is one of the rotated forms of mat[][], it must have at least one occurrence of the original or the reversed form of mat[][]’s rows and columns present as its rows and columns.
请按照以下步骤解决问题:
- 如果mat[][]和T[][] 的尺寸不相等,则打印“ No ”并返回。
- 初始化一个向量映射,比如m来存储所有行、列及其反转版本的频率。
- 在 [0, M-1]范围内迭代并使用变量i并执行以下步骤:
- 将m[mat[i]]增加1 ,然后反转向量mat[i] ,然后将m[mat[i]]增加1 。
- 在 [0, N-1]范围内迭代并使用变量i并执行以下步骤:
- 推在载体中的发言权r处的第i列中的所有元素。
- 将m[r]增加1 ,然后反转向量r ,然后将m[r]增加1。
- 在 [0, M-1]范围内迭代并使用变量i并执行以下步骤:
- 如果m[T[i]]小于0,则打印“ No ”并返回。
- 否则,将m[T[i]]减 1 。
- 在 [0, N-1]范围内迭代并使用变量i并执行以下步骤:
- 推在载体中的发言权r处的T [] []的第i列中的所有元素。
- 如果m[r]小于 0,则打印“ No ”并返回。
- 否则,将m[r]减 1 。
- 最后,如果以上情况都不满足,则打印“是”。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check whether another
// matrix can be created by rotating
// mat one or more times by 90 degrees
string findRotation(vector >& mat,
vector >& T)
{
// If the dimensions of both the
// arrays don't match
if (T.size() != mat.size()
|| T[0].size() != mat[0].size()) {
// Return false
return "No";
}
// Map to store all rows, columns
// and their reversed versions
map, int> m;
// Iterate in the range [0, M-1]
for (int i = 0; i < mat.size(); i++) {
// Increment the frequency of the
// i'th row by 1
m[mat[i]] += 1;
// Reverse the i'th row
reverse(mat[i].begin(), mat[i].end());
// Increment the frequency of the
// i'th row by 1
m[mat[i]] += 1;
}
// Iterate in the range [0, N-1]
for (int i = 0; i < mat[0].size(); i++) {
// Stores the i'th column
vector r = {};
// Iterate in the range [0, M-1]
for (int j = 0; j < mat.size(); j++) {
r.push_back(mat[j][i]);
}
// Increment the frequency of the
// i'th column by 1
m[r] += 1;
// Reverse the i'th column
reverse(r.begin(), r.end());
// Increment the frequency of the
// i'th column by 1
m[r] += 1;
}
// Iterate in the range [0, M-1]
for (int i = 0; i < T.size(); i++) {
// If frequency of the i'th row
// is more in T[][] than in the
// mat[][].
if (m[T[i]] <= 0) {
return "No";
}
// Decrement the frequency of the
// i'th row by 1
m[T[i]] -= 1;
}
// Iterate in the range [0, N-1]
for (int i = 0; i < T[0].size(); i++) {
// Stores the ith column
vector r = {};
// Iterate in the range [0, M-1]
for (int j = 0; j < T.size(); j++) {
r.push_back(T[j][i]);
}
// If frequency of the i'th column
// is more in T[][] than in mat[][].
if (m[r] <= 0) {
return "No";
}
// Decrement the frequency of the i'th
// column by 1
m[r] -= 1;
}
// Return "Yes"
return "Yes";
}
// Driver code
int main()
{
// Input
vector > mat
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
vector > T
= { { 3, 6, 9 }, { 2, 5, 8 }, { 1, 4, 7 } };
// Function call
cout << findRotation(mat, T);
return 0;
}
Javascript
Yes
时间复杂度: O(N*M)
辅助空间: (N*M)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。