[LeetCode] 840. Magic Squares In Grid_Easy

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 an grid of integers, how many 3 x 3 "magic square" subgrids are there?  (Each subgrid is contiguous).

Example 1:

Input: [[4,3,8,4],
        [9,5,1,9],
        [2,7,6,2]]
Output: 1
Explanation:
The following subgrid is a 3 x 3 magic square:
438
951
276

while this one is not:
384
519
762

In total, there is only one magic square inside the given grid.

Note:

  1. 1 <= grid.length <= 10
  2. 1 <= grid[0].length <= 10
  3. 0 <= grid[i][j] <= 15

思路就是依次扫, 只是利用

Here I just want share two observatons with this 1-9 condition:

Assume a magic square:
a1,a2,a3
a4,a5,a6
a7,a8,a9

a2 + a5 + a8 = 15
a4 + a5 + a6 = 15
a1 + a5 + a9 = 15
a3 + a5 + a7 = 15

Accumulate all, then we have:
sum(ai) + 3 * a5 = 60
3 * a5 = 15
a5 = 5

The center of magic square must be 5.  这个条件来去减少一些判断.

Code:

class Solution:
    def numMagicSquaresInside(self, grid):
        ans, lrc = 0, [len(grid), len(grid[0])]
        def checkMagic(a,b, c, d, e, f, g ,h, i):
            return (sorted([a,b,c,d,e,f,g,h,i]) == [i for i in range(1,10)] and
                   (a + b+c == d + e + f == g + h + i == a + d + g == b + e + h == c +f + i
                   == a + e + i == c +  e + g == 15))
        for i in range(1, lrc[0]-1):
            for j in range(1, lrc[1]-1):
                if grid[i][j] == 5 and checkMagic(grid[i-1][j-1], grid[i-1][j], grid[i-1][j+1],
                             grid[i][j-1], grid[i][j], grid[i][j+1],
                             grid[i+1][j-1], grid[i+1][j], grid[i+1][j+1]):
                    ans += 1
        return ans

原文地址:https://www.cnblogs.com/Johnsonxiong/p/9547361.html

时间: 2024-08-15 08:37:14

[LeetCode] 840. Magic Squares In Grid_Easy的相关文章

【Leetcode_easy】840. Magic Squares In Grid

problem 840. Magic Squares In Grid 参考 1. Leetcode_easy_840. Magic Squares In Grid; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11214881.html

840. Magic Squares In Grid (5月27日)

开头 这是每周比赛中的第一道题,博主试了好几次坑后才勉强做对了,第二道题写的差不多结果去试时结果比赛已经已经结束了(尴尬),所以今天只记录第一道题吧 题目原文 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.

840. Magic Squares In Grid ——weekly contest 86

题目链接:https://leetcode.com/problems/magic-squares-in-grid/description attention:注意给定的数字不一定是1-9. time:5ms 本人的解法过于粗糙,看出了中间必须是5,然后比较每行每列每对角线的值是否相等. class Solution { public: int numMagicSquaresInside(vector<vector<int>>& grid) { int n = grid.si

840. Magic Squares In Grid

1 class Solution 2 { 3 public: 4 int numMagicSquaresInside(vector<vector<int>>& grid) 5 { 6 int count=0; 7 int szx=grid.size(); 8 int szy=grid[0].size(); 9 for(int i=0;i<szx-2;i++) 10 { 11 for(int j=0;j<szy-2;j++) 12 { 13 if(grid[i+1

BFS解Magic Squares

Magic Squares IOI'96 Following the success of the magic cube, Mr. Rubik invented its planarversion, called magic squares. This is a sheet composed of 8 equal-sizedsquares: 1 2 3 4 8 7 6 5 In this task we consider the version where each square has a d

洛谷 P2730 魔板 Magic Squares

P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有一种颜色.这8种颜色用前8个正整数来表示.可以用颜色的序列来表示一种魔板状态,规定从魔板的左上角开始,沿顺时针方向依次取出整数,构成一个颜色序列.对于上图的魔板状态,我们用序列(1,2,3,4,5,6,7,8)来表示.这是基本状态. 这里提供三种基本操作,分别用大写字母“

洛谷P2730 魔板 Magic Squares

P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有一种颜色.这8种颜色用前8个正整数来表示.可以用颜色的序列来表示一种魔板状态,规定从魔板的左上角开始,沿顺时针方向依次取出整数,构成一个颜色序列.对于上图的魔板状态,我们用序列(1,2,3,4,5,6,7,8)来表示.这是基本状态. 这里提供三种基本操作,分别用大写字母“

魔板 Magic Squares

[题目描述]: 魔板 Magic Squares [思路]: 是不是感觉和八数码有点像? 显而易见的宽搜,把魔板的状态表示为排列,则状态最多有\(8! = 40320\)种,空间是可以接受的,对于是第几个排列可以用康拓展开来实现(我想在做八数码的时候你们都深知这个套路),然后根据题目中的三种方式转移状态,每个状态转移出\(3\)个子状态,注意判重!,一旦目标状态出现,那个所搜索的层数一定是能得到该状态的最小步数.最后就是代码细节多,要仔细. #include<cstdio> #include&

Luogu P2730 魔板 Magic Squares

P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有一种颜色.这8种颜色用前8个正整数来表示.可以用颜色的序列来表示一种魔板状态,规定从魔板的左上角开始,沿顺时针方向依次取出整数,构成一个颜色序列.对于上图的魔板状态,我们用序列(1,2,3,4,5,6,7,8)来表示.这是基本状态. 这里提供三种基本操作,分别用大写字母"