[LeetCode] Magic Squares In Grid

840. Magic Squares In Grid

A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.

Given a row x col grid of integers, how many 3 x 3 “magic square” subgrids are there? (Each subgrid is contiguous).

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
class Solution {
public:
int numMagicSquaresInside(vector<vector<int>>& grid) {
int n(grid.size()), m(grid[0].size()), res(0), cmp((1<<10) - 2), vals[9];
if(n < 2 or m < 2) return res;
for(int i = 0; i < n - 2; ++i) {
for(int j = 0; j < m - 2; ++j) {
vals[0] = grid[i][j] + grid[i + 1][j] + grid[i + 2][j];
vals[1] = grid[i][j + 1] + grid[i + 1][j + 1] + grid[i + 2][j + 1];
vals[2] = grid[i][j + 2] + grid[i + 1][j + 2] + grid[i + 2][j + 2];

vals[3] = grid[i][j] + grid[i][j + 1] + grid[i][j + 2];
vals[4] = grid[i + 1][j] + grid[i + 1][j + 1] + grid[i + 1][j + 2];
vals[5] = grid[i + 2][j] + grid[i + 2][j + 1] + grid[i + 2][j + 2];

vals[6] = grid[i][j] + grid[i + 1][j + 1] + grid[i + 2][j + 2];
vals[7] = grid[i][j + 2] + grid[i + 1][j + 1] + grid[i + 2][j];

vals[8] = (1<<grid[i][j]) | (1<<grid[i][j + 1]) | (1<<grid[i][j + 2]) |
(1<<grid[i + 1][j]) | (1<<grid[i + 1][j + 1]) | (1<<grid[i + 1][j + 2]) |
(1<<grid[i + 2][j]) | (1<<grid[i + 2][j + 1]) | (1<<grid[i + 2][j + 2]);

if (vals[0] == vals[1] and vals[1] == vals[2] and vals[2] == vals[3] and vals[3] == vals[4] and vals[4] == vals[5] and
vals[5] == vals[6] and vals[6] == vals[7] and vals[8] == cmp){
res++;
}
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/01/18/PS/LeetCode/magic-squares-in-grid/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.