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
 

这道题定义了一种神奇正方形,是一个3x3大小,且由1到9中到数字组成,各行各列即对角线和都必须相等。那么其实这个神奇正方形的各行各列及对角线之和就已经被限定了,必须是15才行,而且最中间的位置必须是5,否则根本无法组成满足要求的正方形。博主也没想出啥特别巧妙的方法,就老老实实的遍历所有的3x3大小的正方形呗,我们写一个子函数来检测各行各列及对角线的和是否为15,在调用子函数之前,先检测一下中间的数字是否为5,是的话再进入子函数。在子函数中,先验证下该正方形中的数字是否只有1到9中的数字,且不能由重复出现,使用一个一维数组来标记出现过的数字,若当前数字已经出现了,直接返回true。之后便是一次计算各行各列及对角线之和是否为15了,若全部为15,则返回true,参见代码如下:

class Solution {
public:
int numMagicSquaresInside(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[].size(), res = ;
for (int i = ; i < m - ; ++i) {
for (int j = ; j < n - ; ++j) {
if (grid[i + ][j + ] == && isValid(grid, i, j)) ++res;
}
}
return res;
}
bool isValid(vector<vector<int>>& grid, int i, int j) {
vector<int> cnt();
for (int x = i; x < i + ; ++x) {
for (int y = j; y < j + ; ++y) {
int k = grid[x][y];
if (k < || k > || cnt[k] == ) return false;
cnt[k] = ;
}
}
if ( != grid[i][j] + grid[i][j + ] + grid[i][j + ]) return false;
if ( != grid[i + ][j] + grid[i + ][j + ] + grid[i + ][j + ]) return false;
if ( != grid[i + ][j] + grid[i + ][j + ] + grid[i + ][j + ]) return false;
if ( != grid[i][j] + grid[i + ][j] + grid[i + ][j]) return false;
if ( != grid[i][j + ] + grid[i + ][j + ] + grid[i + ][j + ]) return false;
if ( != grid[i][j + ] + grid[i + ][j + ] + grid[i + ][j + ]) return false;
if ( != grid[i][j] + grid[i + ][j + ] + grid[i + ][j + ]) return false;
if ( != grid[i + ][j] + grid[i + ][j + ] + grid[i][j + ]) return false;
return true;
}
};

参考资料:

https://leetcode.com/problems/magic-squares-in-grid/

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Magic Squares In Grid 网格中的神奇正方形的更多相关文章

  1. Leetcode840.Magic Squares In Grid矩阵中的幻方

    3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 & ...

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

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

  3. 【Leetcode_easy】840. Magic Squares In Grid

    problem 840. Magic Squares In Grid solution: class Solution { public: int numMagicSquaresInside(vect ...

  4. LeetCode算法题-Magic Squares In Grid(Java实现)

    这是悦乐书的第326次更新,第349篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第196题(顺位题号是840).3 x 3魔方是一个3 x 3网格,填充了从1到9的不同 ...

  5. C#LeetCode刷题之#840-矩阵中的幻方(Magic Squares In Grid)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3752 访问. 3 x 3 的幻方是一个填充有从 1 到 9 的不 ...

  6. [Swift]LeetCode840. 矩阵中的幻方 | 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, co ...

  7. 【LeetCode】840. Magic Squares In Grid 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 利用河图规律 暴力解法 日期 题目地址:https: ...

  8. 840. Magic Squares In Grid ——weekly contest 86

    题目链接:https://leetcode.com/problems/magic-squares-in-grid/description attention:注意给定的数字不一定是1-9. time: ...

  9. 840. Magic Squares In Grid

    class Solution { public: int numMagicSquaresInside(vector<vector<int>>& grid) { ; in ...

随机推荐

  1. mac 删除文件夹里所有的.svn文件

    先用命令行,进入你要删除的文件夹中(./ 为这个文件夹的当前路径,也可以填写绝对路径) 命令行下输入: sudo find ./ -name ".svn" -exec rm -r ...

  2. 题解-UOJ 455雪灾与外卖

    Problem \(\mathrm{UOJ~455}\) 题意概要:一根数轴上有 \(n\) 只老鼠与 \(m\) 个洞,每个洞有费用与容量限制,要求每只老鼠要进一个洞且每个洞的老鼠不超过自身的容量限 ...

  3. zabbix3.2利用自动发现功能对fastcgi模式的php状态进行集中监控

    zabbix3.2利用自动发现功能对fastcgi模式的php状态进行集中监控 前端nginx虚拟主机引用后端多个php接口,为了方便监控,将后端服务器集中配置在nginx中,具体配置如下: [roo ...

  4. Springboot+Mybaits之两张表同时插入数据

    项目需求是,一张表添加数据的同时,另外一张表也需要添加数据,话不多说,直接上代码. 1.Controller,我把两个DTO直接放到一个@RequestBody中.其中throws是后台获取当前时间抛 ...

  5. NOIP2018Day1T2 货币系统

    题目描述 在网友的国度中共有 \(n\) 种不同面额的货币,第 \(i\) 种货币的面额为 \(a[i]\),你可以假设每一种货币都有无穷多张.为了方便,我们把货币种数为 \(n\).面额数组为 \( ...

  6. router-link 返回上页 和 新窗口打开链接

    1.如果使用了Vue-router的话,就可以用 this.$router.go(-1) 实现返回: 2.如果没使用vue-router,就可以用 window.history.go(-1) 实现返回 ...

  7. shell 其他杂项知识点笔记

    // 其他杂项知识点 对于普通用户,Base shell 默认的提示符是美元符号$:对于超级用户(root 用户),Bash Shell 默认的提示符是井号# ------- \#!是一个约定的标记, ...

  8. ubuntu快速部署gitlab汉化容器

    前言:gitlab的原理我就不扯了(看这个https://www.jianshu.com/p/567207ac51cd),下面直接上操作 1.前提: a.要有docker的运行环境,用service ...

  9. 2018年最新JAVA面试题总结之数据库(3)

    转自于:https://zhuanlan.zhihu.com/p/39804394 1.MySQL的delete与truncate区别? 回答:delete语句执行删除的过程是每次从表中删除一行,并且 ...

  10. ThoughtWorks的面试总结

    今天有幸到ThoughtWorks去面试.我觉得自己的能力与他们的需要有些距离(还不知道面试结果如何). 逻辑测试部分,不是很难,是语言问题.几乎用了1个小时去理解一句表达.在Pair Program ...