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)


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