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.size();
        int res = 0;
        for(int i = 0; i < n - 2; i++){
            for(int j = 0; j < n - 2; j++){
                int sign = 0;
                for(int k = i; k < i+3; k++){
                    for(int l = j; l < j+3; l++){
                        if(grid[k][l] > 9 || grid[k][l] < 1){
                            sign = 1;
                            break;
                        }
                    }
                    if(sign){
                        break;
                    }
                }
                if(sign){
                    continue;
                }
                if(grid[i+1][j+1] == 5){

                    if(grid[i][j]+grid[i+2][j+2]==grid[i+2][j]+grid[i][j+2]&&grid[i+2][j]+grid[i][j+2]==grid[i+1][j]+grid[i+1][j+2]&&grid[i+2][j]+grid[i][j+2]==grid[i][j+1]+grid[i+2][j+1]){

                        if(grid[i][j]+grid[i+1][j]+grid[i+2][j]==grid[i][j+2]+grid[i+1][j+2]+grid[i+2][j+2]&&grid[i][j]+grid[i+1][j]+grid[i+2][j]==15){
                            if(grid[i][j]+grid[i][j+1]+grid[i][j+2]==grid[i+2][j]+grid[i+2][j+1]+grid[i+2][j+2]&&grid[i][j]+grid[i][j+1]+grid[i][j+2]==15){
                                res++;
                            }
                        }
                    }
                }
            }
        }
        return res;
    }
};

下面是看到的别人写的更简单的实现:

https://leetcode.com/problems/magic-squares-in-grid/discuss/133874/Python-5-and-43816729

原文地址:https://www.cnblogs.com/jinjin-2018/p/9098125.html

时间: 2024-10-08 16:05:56

840. Magic Squares In Grid ——weekly contest 86的相关文章

【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

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

Leetcode Weekly Contest 86

Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 "幻方" 子矩阵?(每个子矩阵都是连续的). 直接模拟即可,本来是签到题,由于粗心,浪费了时间. 1 class Solution { 2 public: 3 int numMagicSquaresInside(vector&l

[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

841. Keys and Rooms —— weekly contest 86

题目链接:https://leetcode.com/problems/keys-and-rooms/description/ 简单DFS time:9ms 1 class Solution { 2 public: 3 void DFS(int root,vector<int>& visited,vector<vector<int>>& rooms){ 4 visited[root] = 1; 5 for(auto x : rooms[root]){ 6

842. Split Array into Fibonacci Sequence —— weekly contest 86

题目链接:https://leetcode.com/problems/split-array-into-fibonacci-sequence/description/ 占坑. string 的数值转换函数(c++11)介绍 :https://blog.csdn.net/calmreason/article/details/41204211 答案可参考:https://leetcode.com/problems/split-array-into-fibonacci-sequence/discuss

843. Guess the Word —— weekly contest 86

题目链接:https://leetcode.com/problems/guess-the-word/description/ 占坑 据说要用启发式算法,可参考下述答案进行学习:https://leetcode.com/problems/guess-the-word/discuss/133862/Random-Guess-and-Minimax-Guess-with-Comparison 原文地址:https://www.cnblogs.com/jinjin-2018/p/9098158.html

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