-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1886. Determine Whether Matrix Can Be Obtained By Rotation.cpp
74 lines (65 loc) · 2.08 KB
/
1886. Determine Whether Matrix Can Be Obtained By Rotation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//problem 1886. Determine Whether Matrix Can Be Obtained By Rotation
// Given two n x n binary matrices mat and target,
// return true if it is possible to make mat equal to target by rotating mat in 90-degree increments,
// or false otherwise.
// Example 1:
// Input: mat = [[0,1],[1,0]], target = [[1,0],[0,1]]
// Output: true
// Explanation: We can rotate mat 90 degrees clockwise to make mat equal target.
// Example 2:
// Input: mat = [[0,1],[1,1]], target = [[1,0],[0,1]]
// Output: false
// Explanation: It is impossible to make mat equal to target by rotating mat.
// Example 3:
// Input: mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]]
// Output: true
// Explanation: We can rotate mat 90 degrees clockwise two times to make mat equal target.
// Constraints:
// n == mat.length == target.length
// n == mat[i].length == target[i].length
// 1 <= n <= 10
// mat[i][j] and target[i][j] are either 0 or 1.
class Solution
{
public:
void rotate(vector<vector<int>> &arr)
{
int n = arr[0].size();
for (int i = 0; i < n / 2; i++)
{
for (int j = i; j < n - i - 1; j++)
{
int temp = arr[i][j];
arr[i][j] = arr[n - 1 - j][i];
arr[n - 1 - j][i] = arr[n - 1 - i][n - 1 - j];
arr[n - 1 - i][n - 1 - j] = arr[j][n - 1 - i];
arr[j][n - 1 - i] = temp;
}
}
}
bool check(vector<vector<int>> &mat, vector<vector<int>> target)
{
for (int i = 0; i < mat.size(); i++)
{
for (int j = 0; j < mat.size(); j++)
if (mat[i][j] != target[i][j])
return false;
}
return true;
}
bool findRotation(vector<vector<int>> &mat, vector<vector<int>> &target)
{
if (check(mat, target))
return 1;
rotate(mat);
if (check(mat, target))
return 1;
rotate(mat);
if (check(mat, target))
return 1;
rotate(mat);
if (check(mat, target))
return 1;
return false;
}
};