-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2596.cpp
More file actions
29 lines (28 loc) · 993 Bytes
/
2596.cpp
File metadata and controls
29 lines (28 loc) · 993 Bytes
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
class Solution {
public:
vector<pair<int, int>> directions = {{1, 2}, {-1, 2}, {1, -2}, {-1, -2}, {2, 1}, {2, -1}, {-2, 1}, {-2, -1}};
bool checkValidGrid(vector<vector<int>>& grid) {
int n = grid.size();
vector<pair<int, int>> locations(n * n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
locations[grid[i][j]] = {i, j};
}
}
int currentX = 0;
int currentY = 0;
for (int i = 1; i < n * n; ++i) {
int diffX = locations[i].first - currentX;
int diffY = locations[i].second - currentY;
pair<int, int> p = {diffX, diffY};
bool have = false;
for (auto& direction : directions) {
if (p == direction) have = true;
}
if (!have) return false;
currentX = locations[i].first;
currentY = locations[i].second;
}
return true;
}
};