Skip to content

Commit

Permalink
2024-08-11 Magic Squares In Grid
Browse files Browse the repository at this point in the history
  • Loading branch information
InSange committed Aug 10, 2024
1 parent 5fd4958 commit 3fae6de
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
| 23μ°¨μ‹œ | 2024.08.03 | μŠ¬λΌμ΄λ”© μœˆλ„μš° | [Minimum Swaps to Group All 1's Together 2](https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii/) | [#23](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/82)]
| 24μ°¨μ‹œ | 2024.08.04 | BFS | [트리](https://www.acmicpc.net/problem/1068) | [#24](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/83)]
| 25μ°¨μ‹œ | 2024.08.10 | BFS | [Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/) | [#25](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/87)]
| 26μ°¨μ‹œ | 2024.08.11 | μˆ˜ν•™ | [Magic Squares In Grid](https://leetcode.com/problems/magic-squares-in-grid/) | [#26](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/89)]
---

https://leetcode.com/problems/robot-collisions/
55 changes: 55 additions & 0 deletions InSange/μˆ˜ν•™/840_Magic Squares In Grid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <vector>

using namespace std;

class Solution {
public:
bool Check(int r, int c, vector<vector<int>>& grid)
{
vector<bool> isCheck(10, false);

for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
int num = grid[r + i][c + j];
if (num < 1 || num > 9) return false;
if (isCheck[num]) return false;
isCheck[num] = true;
}
}

int standard_num = grid[r][c] + grid[r + 1][c + 1] + grid[r + 2][c + 2];
if (standard_num != grid[r][c + 2] + grid[r + 1][c + 1] + grid[r + 2][c]) return false;

for (int i = 0; i < 3; i++)
{
if (standard_num != (grid[r + i][c] + grid[r + i][c + 1] + grid[r + i][c + 2])) return false;
}

for (int i = 0; i < 3; i++)
{
if (standard_num != (grid[r][c + i] + grid[r + 1][c + i] + grid[r + 2][c + i])) return false;
}

return true;
}

int numMagicSquaresInside(vector<vector<int>>& grid) {
int col = grid[0].size(), row = grid.size();

if (col < 3 || row < 3) return 0;

int ans = 0;

for (int i = 0; i < row - 2; i++)
{
for (int j = 0; j < col - 2; j++)
{
if (Check(i, j, grid)) ans++;
}
}

return ans;
}
};

0 comments on commit 3fae6de

Please sign in to comment.