[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 <= 101 <= grid[0].length <= 100 <= 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 = 15a4 + a5 + a6 = 15a1 + a5 + a9 = 15a3 + a5 + a7 = 15
Accumulate all, then we have:sum(ai) + 3 * a5 = 603 * a5 = 15a5 = 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 我们知道魔板的每一个方 ...
随机推荐
- 数据库高分笔记01:事务ACID
作者:arccosxy 转载请注明出处:http://www.cnblogs.com/arccosxy/ 事务就是一组原子性的SQL查询,或者说一个独立的工作单元.如果数据库引擎能够成功地对数据库应 ...
- Nand: OOB BBT ECC PEB LEB
OBB: 例如Samsung K9F1208U0B,数据存储容量为64MB,采用块页式存储管理.8个I/O 引脚充当数据.地址.命令的复用端口.芯片内部存储布局及存储操作特点: 一片Nand flas ...
- CentOs安装和使用
● 去掉图形界面 http://blog.csdn.net/op_zoro/article/details/44993881 ● centos 7覆盖windows vi /boot/grub2/gr ...
- 洛谷 P1181数列分段SectionI 【贪心】
题目描述 对于给定的一个长度为NN的正整数数列A_iAi,现要将其分成连续的若干段,并且每段和不超过MM(可以等于MM),问最少能将其分成多少段使得满足要求. 输入输出格式 输入格式: 第1行包含两 ...
- Only the storage referenced by ptr is modified. No other storage locations are accessed by the call.
free - C++ Reference http://www.cplusplus.com/reference/cstdlib/free/ Data races Only the storage re ...
- [knowledge][模式匹配] 字符匹配/模式匹配 正则表达式 自动机
字符串 T = abcabaabcabac,字符串 P = abaa,判断P是否是T的子串,就是字符串匹配问题了,T 叫做文本(Text) ,P 叫做模式(Pattern),所以正确描述是,找出所有在 ...
- [skill][telnet] 用telnet获取一个网页
一直也搞不懂, telnet到底是干嘛用的. 然而, 它可以得到一个网页. /home/tong/Data/performance_test [tong@T7] [:] > telnet nyu ...
- flash插件如何生成
1.通过flash 原生的代码开发插件界面: 2.对于需要使用到的文件,表现mxi 文件, 该文件为adobe extension infomation file, 示例如下 <macromed ...
- MyBati__mapper 中取值(#{} 或${}) 以及 parameterType为(基本类型 或复杂类型)
参考资料: MyBatis学习笔记(三)——parameterType为基本类型时的使用方法 MyBatis的传入参数parameterType类型 1. MyBatis的传入参数parameterT ...
- Java之旅_高级教程_实例_文件操作
1.文件写入 import java.io.*; public class MainClass{ public static void main(String[] args){ try{ Buffer ...