[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 <= grid.length <= 10
1 <= grid[0].length <= 10
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
[LeetCode] 840. Magic Squares In Grid_Easy的更多相关文章
- 【Leetcode_easy】840. Magic Squares In Grid
problem 840. Magic Squares In Grid solution: class Solution { public: int numMagicSquaresInside(vect ...
- 840. Magic Squares In Grid (5月27日)
开头 这是每周比赛中的第一道题,博主试了好几次坑后才勉强做对了,第二道题写的差不多结果去试时结果比赛已经已经结束了(尴尬),所以今天只记录第一道题吧 题目原文 Magic Squares In Gri ...
- 【LeetCode】840. Magic Squares In Grid 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 利用河图规律 暴力解法 日期 题目地址:https: ...
- 840. Magic Squares In Grid ——weekly contest 86
题目链接:https://leetcode.com/problems/magic-squares-in-grid/description attention:注意给定的数字不一定是1-9. time: ...
- 840. Magic Squares In Grid
class Solution { public: int numMagicSquaresInside(vector<vector<int>>& grid) { ; in ...
- USACO 3.2 Magic Squares
Magic SquaresIOI'96 Following the success of the magic cube, Mr. Rubik invented its planar version, ...
- 洛谷 P2730 魔板 Magic Squares 解题报告
P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 ...
- 哈希+Bfs【P2730】 魔板 Magic Squares
没看过题的童鞋请去看一下题-->P2730 魔板 Magic Squares 不了解康托展开的请来这里-->我这里 至于这题为什么可以用康托展开?(瞎说时间到. 因为只有8个数字,且只有1 ...
- 3.2.5 Magic Squares 魔板
3.2.5 Magic Squares 魔板 成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 我们知道魔板的每一个方 ...
随机推荐
- JQuery登录代码
$(function () { $("#login").submit(function(event) { event.preventDefault(); if ($("# ...
- 思科交换机根据mac地址限制主机
思科交换机可以根据客户端mac地址限制访问 查看对应IP的mac地址及vlan show arp | in 10.1.62.188 进入设置模式 configure terminal mac addr ...
- Python学习笔记之--我又开始学习Python了(随时更新)
2019.02.09 更新 Python 学习计划已经开始几天了,跟着一本叫<Django for beginner>的书籍在学习用Django搭建自己的第一个网站,目前已经进行到第三章, ...
- 被Entity Framework Core的细节改进震撼了一下
今天用 SQL Server Profiler 查看 Entity Framework Core 生成的 SQL 语句时,突然发现一个细节改进,并且被它震撼了一下: exec sp_executesq ...
- POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...
- Lombok安装及使用
为什么要使用Lombok: 虽然一般的IDE(eclipse,Intellij)都有自动生成代码的功能,如:生成setter,getter,toString,equels,hashcode等.但是如果 ...
- 如何使用ffmpeg
https://blog.csdn.net/minger1202/article/details/52468986 解码 https://www.jianshu.com/p/c6cfe2edd083 ...
- log4j.properties 日志文件的详细配置说明
一.在一个web 项目中,使用tomcat 启动通常会在控制台输出出现一个警告信息: 通常为未添加 log4j.properties文件的原因. 二.下面以一个普通的maven项目为例说明一下 1. ...
- Page11:状态反馈、输出反馈的概念及性能比较,极点配置的基本概念、意义及其算法[Linear System Theory]
内容包含离散时间线性时不变系统的稳定判据 状态反馈.输出反馈的基本概念及其性能比较 极点配置的基本概念.意义及其算法
- [knowledge][lisp] lisp与AI
https://blog.youxu.info/2009/08/31/lisp-and-ai-1/ https://blog.youxu.info/2010/02/10/lisp-and-ai-2/