leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2596.cpp (820B)


0 class Solution {
1 public:
2 bool checkValidGrid(const vector<vector<int>> &grid) const {
3 static const int offset_x[] = {-2, -2, -1, -1, 1, 1, 2, 2};
4 static const int offset_y[] = {-1, 1, -2, 2, -2, 2, -1, 1};
5 const int n = size(grid), m = size(grid[0]);
7 int x = 0, y = 0;
8 if (grid[0][0] != 0) return false;
9 for (int cnt = 1; cnt < n * m; cnt++) {
10 for (int k = 0; k < 8; k++) {
11 const int a = x + offset_x[k];
12 const int b = y + offset_y[k];
13 if (a < 0 || b < 0 || a >= n || b >= m) continue;
14 if (grid[a][b] == cnt) {
15 x = a, y = b;
16 goto next;
17 }
18 }
19 return false;
20 next:;
21 }
22 return true;
23 }
24 };