3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等。

给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 “幻方” 子矩阵?(每个子矩阵都是连续的)。

示例 1:

输入: [[4,3,8,4], [9,5,1,9], [2,7,6,2]] 输出: 1 解释: 下面的子矩阵是一个 3 x 3 的幻方: 438 951 276 而这一个不是: 384 519 762 总的来说,在本示例所给定的矩阵中只有一个 3 x 3 的幻方子矩阵。

提示:

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

注意题目中说的是1到9的数字

class Solution {
public:
int numMagicSquaresInside(vector<vector<int> >& grid) {
int res = 0;
int r = grid.size();
int c = grid[0].size();
for(int i = 0; i <= r - 3; i++)
{
for(int j = 0; j <= c - 3; j++)
{
bool check = true;
int flag = grid[i][j] + grid[i][j + 1] + grid[i][j + 2];
if( grid[i + 1][j] + grid[i + 1][j + 1] + grid[i + 1][j + 2] != flag
||grid[i + 2][j] + grid[i + 2][j + 1] + grid[i + 2][j + 2] != flag
||grid[i][j] + grid[i + 1][j + 1] + grid[i + 2][j + 2] != flag
||grid[i + 2][j] + grid[i + 1][j + 1] + grid[i][j + 2] != flag)
{
check = false;
}
for(int y = i; y <= i + 2; y++)
for(int x = j; x <= j + 2; x++)
if(grid[y][x] > 9 || grid[y][x] <= 0)
{
check = false;
break;
}
if(check)
res++;
}
}
return res;
}
};

Leetcode840.Magic Squares In Grid矩阵中的幻方的更多相关文章

  1. [LeetCode] 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 ...

  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. [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 ...

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

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

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

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

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

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

  8. 840. Magic Squares In Grid

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

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

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

随机推荐

  1. JMETER远程运行_多机联合负载

    JMETER远程运行_多机联合负载 远程运行是用一台JMeter控制机控制远程的多台机器来产生负载.控制机与负载机之间通过RMI方式来完成通信.在负载机上运行Agent程序(启动命令是%JMETER_ ...

  2. maven-home

    E:/Soft/Maven/apache-maven-3.3.3 E:\Soft\Maven\apache-maven-3.3.3\conf\settings.xml E:\DellWork\Mave ...

  3. Django项目:CRM(客户关系管理系统)--30--22PerfectCRM实现King_admin数据添加

    登陆密码设置参考 http://www.cnblogs.com/ujq3/p/8553784.html # king_urls.py # ————————02PerfectCRM创建ADMIN页面—— ...

  4. laravel--laravel的重定向类Redirector

    laravel的重定向类Redirector 在laravel5中,重定向类可以直接通过redirect()方法直接获取,不需要声明,有几个常用的方法: redirect() -> to( “重 ...

  5. Hackerrank--Ashton and String(后缀数组)

    题目链接 Ashton appeared for a job interview and is asked the following question. Arrange all the distin ...

  6. nvm与nrm

    nvm:切换Node版本   nrm:管理npm包的镜像源 nvm的安装,可以参考官网,windows系统直接https://github.com/coreybutler/nvm-windows/re ...

  7. linux把普通用户添加到sudo组

    一.linux下把普通用户添加到sudo组的方式: 1. root权限下, 先cd到/etc目录下 2. 由于sudoers文件为只读权限,所以需要添加写入权限,chmod u+w sudoers 3 ...

  8. angular4 路由重用策略 RouterReuseStrategy

    单页面应用现在是主流,随之而来的缺点:页面间切换时不能保存状态 angular4出了一个RouteReuseStrategy路由重用策略可以让组件所有的state和渲染好的html存起来,然后在切回去 ...

  9. win10 下安装 neo4j

    1.neo4j介绍 neo4j是基于Java语言编写图形数据库.图是一组节点和连接这些节点的关系.图形数据库也被称为图形数据库管理系统或GDBMS.详细介绍可看Neo4j 教程 2.安装Java jd ...

  10. Java中用JXL导出Excel代码详解

    jxl是一个韩国人写的java操作excel的工具, 在开源世界中,有两套比较有影响的API可供使用,一个是POI,一个是jExcelAPI.其中功能相对POI比较弱一点.但jExcelAPI对中文支 ...