开头

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

题目原文

  1. 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 an N x N 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 <= grid.length = grid[0].length <= 10
0 <= grid[i][j] <= 15

简单翻译一下

先给出幻方的定义:3×3的矩阵,矩阵中元素由1-9组成,并且每一行、每一列、俩条对角线三个元素之和相等
(ps,我刚开始没看清楚幻方的定义,吃了一些亏)
给定一个N*M的矩阵,其中有多少个三阶幻方?

例子
输入矩阵:[   [4,3,8,4],
            [9,5,1,9],
            [2,7,6,2] ]
输出结果:1
解释
矩阵    [   [4,3,8],
            [9,5,1],
            [2,7,6] ]
是一个三阶幻方

而另一个矩阵:[    [3,8,4],
                 [5,1,9],
                 [7,6,2] ]
不是三阶幻方

备注:

  1. 给定矩阵的维度M×N中,1<=M,N<10
  2. 矩阵中每个元素的值, 0<=a[m][n]<=15

    解答

class Solution {
public:
    int numMagicSquaresInside(vector<vector<int>>& grid) {
        int flag=0;
        vector<int> v{1,2,3,4,5,6,7,8,9};
        if(grid.size()<2||(*grid.begin()).size()<2)
            return flag;
        for(int i=0;i<grid.size()-2;++i){
            for(int j=0;j<(*grid.begin()).size()-2;++j){
                int sum=15;
                if( (grid[i+1][j+1]==5)                              &&
                    (grid[i][j]>=1)&&(grid[i][j]<=9)                 &&
                    (grid[i+1][j]>=1)&&(grid[i+1][j]<=9)             &&
                    (grid[i+2][j]>=1)&&(grid[i+2][j]<=9)              &&
                    (grid[i][j+1]>=1)&&(grid[i][j+1]<=9)              &&
                    (grid[i+2][j+1]>=1)&&(grid[i+2][j+1]<=9)          &&
                    (grid[i][j+2]>=1)&&(grid[i][j+2]<=9)              &&
                    (grid[i+1][j+2]>=1)&&(grid[i][j+2]<=9)            &&
                    (grid[i+2][j+2]>=1)&&(grid[i][j+2]<=9)            &&
                    (grid[i][j]+grid[i][j+1]+grid[i][j+2]==sum)      &&
                    (grid[i+1][j]+grid[i+1][j+1]+grid[i+1][j+2]==sum)  &&
                    (grid[i+2][j]+grid[i+2][j+1]+grid[i+2][j+2]==sum)  &&
                    (grid[i][j]+grid[i+1][j]+grid[i+2][j]==sum)        &&
                    (grid[i][j+1]+grid[i+1][j+1]+grid[i+2][j+1]==sum)  &&
                    (grid[i][j+2]+grid[i+1][j+2]+grid[i+2][j+2]==sum)  &&
                    (grid[i][j]+grid[i+1][j+1]+grid[i+2][j+2]==sum)    &&
                    (grid[i][j+2]+grid[i+1][j+1]+grid[i+2][j]==sum)
                )
                    ++flag;
            }
        }
        return flag;
    }
};

错误分析

第一次

没有考虑到给定的矩阵维度小于3×3

错误例子:
[[8]]

所以才加上了如下这个判断条件:

if(grid.size()<2||(*grid.begin()).size()<2)
            return flag;

第二次

没有注意到三阶幻方的定义中元素是由1-9构成的,之前自认为只要每行每列对角线和相等就行

错误例子1
[   [10,3,5],
    [1,6,11],
    [7,9,2]    ]
每行每列对角线和相等,但和不是15,同时元素不是在1-9内
错误例子2
[   [1,8,6],
    [10,5,0],
    [4,2,9]     ]
每行每列对角线和相等,和是15,但元素不是在1-9内

笔记

  1. 挨个读取对元素,然后进行判断,综上可知,判断是不是三阶幻方的条件如下:
    元素在1-9内+每行每列对角线和相等(=15)
    注:按题目要求所构成的三阶幻方的中心元素必然是5,可优先判别,因为C++判断一系列‘与’构成的逻辑时,只要前面的出错了就不会进行判断后面的条件

840. Magic Squares In Grid (5月27日)的更多相关文章

  1. 【Leetcode_easy】840. Magic Squares In Grid

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

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

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

  3. 840. Magic Squares In Grid

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

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

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

  5. 腾讯QQ认证空间4月27日已全面开放申请,欲进军自媒体

    今天看到卢松松的博客上爆出,腾讯QQ认证空间4月27日已全面开放申请的消息,这一消息出来, 马浩周根据提示方法进行申请,下面先说说腾讯QQ认证空间的申请方法: QQ认证空间开放申请公告地址:http: ...

  6. 2016年12月27日 星期二 --出埃及记 Exodus 21:22

    2016年12月27日 星期二 --出埃及记 Exodus 21:22 "If men who are fighting hit a pregnant woman and she gives ...

  7. 2016年11月27日 星期日 --出埃及记 Exodus 20:18

    2016年11月27日 星期日 --出埃及记 Exodus 20:18 When the people saw the thunder and lightning and heard the trum ...

  8. 2016年10月27日 星期四 --出埃及记 Exodus 19:12

    2016年10月27日 星期四 --出埃及记 Exodus 19:12 Put limits for the people around the mountain and tell them, `Be ...

  9. 2016年6月27日 星期一 --出埃及记 Exodus 14:24

    2016年6月27日 星期一 --出埃及记 Exodus 14:24 During the last watch of the night the LORD looked down from the ...

随机推荐

  1. 前端使用nginx上传文件时,进度获取不对

    在使用iview时,上传文件获取进度时onUploadProgress返回数据不对. 原因是开启了nginx代理,本地上传时先传到本地nginx然后在传到服务器,导致获取进度不对 解决:在nginx的 ...

  2. 初学者需要IPython 与 Jupyter Notebook 吗?

    ipython 是 jupyter notebook的前身并拥有ipython的全部功能         jupyter拥有 cell, markdown 整合的功能, 能同时运行代码, 而且是多组的 ...

  3. MySQL--localhost、局域网、外网访问MySQL

    安装环境: MySQL版本:mysql-installer-community-5.7.22.1.msi,64位 计算机:Windows7旗舰版,64位 本机的局域网IP为:192.168.2.230 ...

  4. 任务十:Flexbox 布局练习

    任务目的 学习如何flex进行布局,学习如何根据屏幕宽度调整布局策略. 任务描述 需要实现的效果如效果图(点击打开)所示,调整浏览器宽度查看响应式效果,红色的文字是说明,不需要写在 HTML 中. 任 ...

  5. 使用SDL2出现 “error LNK2019: 无法解析的外部符号 _SDL_main,该符号在函数 _main 中被引用” 时的可能错误记录

    这几天在使用SDL2,之前一直都没有错,直到上午把项目搬了个地方.结果一直出现 “error LNK2019: 无法解析的外部符号 _SDL_main,该符号在函数 _main 中被引用” . 看了网 ...

  6. 关于实现XX系统设计时所实现的质量属性战术

    可用性: 1)使用Try-catch对抛出的异常进行处理 2)使用Spring事务管理 易用性: 1)在类似删除相关选项时,弹出提示框,防止误操作 2)在不编辑基本信息时,对其进行折叠或者隐藏 3)提 ...

  7. 使用webBrowser下载文件

    如果直接用webBrowser.Navigate("http://***.com/");会弹出文件下载的对话框. 而如果用webclient.UploadData()下载,对方网站 ...

  8. C# Request获取URL常见用法

    如果测试的url地址是http://www.test.com/testweb/default.aspx, 结果如下: Request.ApplicationPath: /testweb Request ...

  9. 实验 MPLS LDP配置

    实验 MPLS LDP配置 一.学习目的 掌握启用和关闭MPLS的方法 掌握启用和关闭MPLS LDP配置方法 掌握使用MPLS LDP配置LSP的方法 二.拓扑图 三.场景 你是公司的网管员,公司的 ...

  10. OID OAM WLS等Oracle 中间件日志位置汇总

    WLS的log:/tip/IMP/bea/user_projects/domains/IDMDomain/servers/AdminServer/logsOID的log:/tip/IMP/bea/us ...