840. Magic Squares In Grid

 1 class Solution
 2 {
 3 public:
 4     int numMagicSquaresInside(vector<vector<int>>& grid)
 5     {
 6         int count=0;
 7         int szx=grid.size();
 8         int szy=grid[0].size();
 9         for(int i=0;i<szx-2;i++)
10         {
11             for(int j=0;j<szy-2;j++)
12             {
13                 if(grid[i+1][j+1]==5&&judge(grid,i,j))
14                     count++;
15             }
16         }
17         return count;
18     }
19
20     bool judge(vector<vector<int>> &grid,int i,int j)
21     {
22         for(int x=i;x<i+3;x++)                 //judge cur num biggerthan 9 or not,if yes,return false
23         {
24             for(int y=j;y<j+3;y++)
25             {
26                 if(grid[x][y]>9)
27                     return false;
28             }
29         }
30         for(int x=i;x<i+3;x++)                //judge every row‘s sum is 15 or not
31         {
32             if(grid[x][j]+grid[x][j+1]+grid[x][j+2]!=15)
33                 return false;
34         }
35         for(int y=j;y<j+3;y++)               //judge every col‘s sum is 15 or not
36         {
37             if(grid[i][y]+grid[i+1][y]+grid[i+2][y]!=15)
38                 return false;
39         }
40         if(grid[i][j]+grid[i+1][j+1]+grid[i+2][j+2]!=15)   //judge diagonals
41             return false;
42         if(grid[i+2][j]+grid[i+1][j+1]+grid[i][j+2]!=15)
43             return false;
44         return true;
45     }
46 };

基本上是暴力解法了,问题不大

原文地址:https://www.cnblogs.com/zhuangbijingdeboke/p/9193209.html

时间: 2024-08-28 00:39:11

840. Magic Squares In Grid的相关文章

【Leetcode_easy】840. Magic Squares In Grid

problem 840. Magic Squares In Grid 参考 1. Leetcode_easy_840. Magic Squares In Grid; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11214881.html

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

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

840. Magic Squares In Grid ——weekly contest 86

题目链接:https://leetcode.com/problems/magic-squares-in-grid/description attention:注意给定的数字不一定是1-9. time:5ms 本人的解法过于粗糙,看出了中间必须是5,然后比较每行每列每对角线的值是否相等. class Solution { public: int numMagicSquaresInside(vector<vector<int>>& grid) { int n = grid.si

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

BFS解Magic Squares

Magic Squares IOI'96 Following the success of the magic cube, Mr. Rubik invented its planarversion, called magic squares. This is a sheet composed of 8 equal-sizedsquares: 1 2 3 4 8 7 6 5 In this task we consider the version where each square has a d

洛谷 P2730 魔板 Magic Squares

P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有一种颜色.这8种颜色用前8个正整数来表示.可以用颜色的序列来表示一种魔板状态,规定从魔板的左上角开始,沿顺时针方向依次取出整数,构成一个颜色序列.对于上图的魔板状态,我们用序列(1,2,3,4,5,6,7,8)来表示.这是基本状态. 这里提供三种基本操作,分别用大写字母“

洛谷P2730 魔板 Magic Squares

P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有一种颜色.这8种颜色用前8个正整数来表示.可以用颜色的序列来表示一种魔板状态,规定从魔板的左上角开始,沿顺时针方向依次取出整数,构成一个颜色序列.对于上图的魔板状态,我们用序列(1,2,3,4,5,6,7,8)来表示.这是基本状态. 这里提供三种基本操作,分别用大写字母“

魔板 Magic Squares

[题目描述]: 魔板 Magic Squares [思路]: 是不是感觉和八数码有点像? 显而易见的宽搜,把魔板的状态表示为排列,则状态最多有\(8! = 40320\)种,空间是可以接受的,对于是第几个排列可以用康拓展开来实现(我想在做八数码的时候你们都深知这个套路),然后根据题目中的三种方式转移状态,每个状态转移出\(3\)个子状态,注意判重!,一旦目标状态出现,那个所搜索的层数一定是能得到该状态的最小步数.最后就是代码细节多,要仔细. #include<cstdio> #include&

Luogu P2730 魔板 Magic Squares

P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有一种颜色.这8种颜色用前8个正整数来表示.可以用颜色的序列来表示一种魔板状态,规定从魔板的左上角开始,沿顺时针方向依次取出整数,构成一个颜色序列.对于上图的魔板状态,我们用序列(1,2,3,4,5,6,7,8)来表示.这是基本状态. 这里提供三种基本操作,分别用大写字母"