0840.cpp (969B)
1 class Solution { 2 public: 3 int numMagicSquaresInside(const vector<vector<int>> &grid) const { 4 const int n = size(grid), m = size(grid[0]); 5 int res = 0; 6 7 for (int i = 1; i < n - 1; i++) { 8 for (int j = 1; j < m - 1; j++) { 9 if (grid[i][j] != 5) continue; 10 11 if (grid[i - 1][j - 1] + grid[i - 1][j] + grid[i - 1][j + 1] != 15) continue; 12 if (grid[i - 1][j - 1] + grid[i][j - 1] + grid[i + 1][j - 1] != 15) continue; 13 14 unordered_set<int> us; 15 for (int k = i - 1; k <= i + 1; k++) { 16 for (int l = j - 1; l <= j + 1; l++) { 17 if (grid[k][l] < 1 || grid[k][l] > 9) goto next; 18 if (us.count(grid[k][l])) goto next; 19 us.insert(grid[k][l]); 20 } 21 } 22 23 res++; 24 next:; 25 } 26 } 27 28 return res; 29 } 30 };