Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

26-InSange #89

Merged
merged 2 commits into from
Aug 25, 2024
Merged

26-InSange #89

merged 2 commits into from
Aug 25, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented Aug 10, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

Magic Squares In Grid

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

2์‹œ๊ฐ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋ฌธ์ œ ๊ฐœ์š”

row์™€ col์ด ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์—์„œ 3x3 ๋ฐฐ์—ด์„ ์ถ”์ถœํ•ด ํ•ด๋‹น ๋ฐฐ์—ด์ด ๋งค์ง ์‚ฌ๊ฐํ˜•์ธ์ง€ ํŒŒ์•…ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.
๋งค์ง ์‚ฌ๊ฐํ˜•์€ 1 ๋ถ€ํ„ฐ 9๊นŒ์ง€ ์ด๋ค„์ ธ ์žˆ์œผ๋ฉฐ ๊ฐ ๋ฐฉํ–ฅ์ด ์ง€๋‹Œ 3๊ฐœ์˜ ์›์†Œ๋“ค์„ ๋”ํ•˜๋ฉด ๋ชจ๋‘ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์‚ฌ๊ฐํ˜•์ด๋‹ค.

์ด ๋งค์ง ์‚ฌ๊ฐํ˜•์ด ๋ช‡ ๊ฐœ ๋‚˜์˜ค๋Š”์ง€ ํŒŒ์•…ํ•˜๋Š” ๋ฌธ์ œ
image

image
ํ•ด๋‹น ์ด๋ฏธ์ง€๋Š” ๋ชจ๋“  ์ง์„  ๋ฐ ๋Œ€๊ฐ์„ ์˜ ํ•ฉ์€ 15๋กœ ์ด๋ค„์ง„๋‹ค. ๊ณ ๋กœ ๋งค์ง ์‚ฌ๊ฐํ˜•์ด๋‹ค.

image
ํ•ด๋‹น ์ด๋ฏธ์ง€๋Š” ์ง์„ ์œผ๋กœ๋Š” 15์˜ ๊ฐ’์„ ์ง€๋‹ˆ์ง€๋งŒ ๋Œ€๊ฐ์„ ์œผ๋กœ๋Š” ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฐ’์„ ๋ณด์ด๊ธฐ์— ๋งค์ง ์‚ฌ๊ฐํ˜•์ด ์•„๋‹ˆ๋‹ค.

๊ณ ๋กœ ๋งค์ง ์‚ฌ๊ฐํ˜•์€ 1๊ฐœ ๋ฟ์ด๋‹ค.

ํ’€์ด ๋ฐฉ์‹

์ผ์ผ์ด ๋‹ค ๋ชจ๋“  ์ง์„ ์„ ํ™•์ธํ•ด๋ณด๋ฉด ๋  ๊ฒƒ์ด๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

ํŽธ์˜์ƒ ์‹œ์ž‘ ์ง€์ ์„ (y, x)๋ผ๊ณ  ์ง€์นญํ–ˆ์„ ๋•Œ ์šฐ๋ฆฌ๋Š” ํ–‰๊ณผ ์—ด์ด 3์ด์ƒ์ธ ๊ฒƒ๋“ค๋งŒ ํŒŒ์•…ํ•˜๋ฉด ๋˜๊ธฐ์— ํ–‰๊ณผ ์—ด์ด 3 ๋ฏธ๋งŒ์ด๋ฉด ๋ฐ”๋กœ 0์„ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.

3 ์ด์ƒ์ผ ๊ฒฝ์šฐ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด grid์˜ ํ–‰๊ณผ ์—ด์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•œ ํ›„ 2๋ฅผ ๋นผ์คŒ์œผ๋กœ์จ 3x3 ์‚ฌ๊ฐํ˜•์ด ๋งŒ๋“ค์–ด ์งˆ ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ ์กฐ๊ฑด์„ ๊ตฌ์„ฑํ•œ ๋’ค ์ˆœ์„œ๋Œ€๋กœ grid์˜ ์›์†Œ ํ•˜๋‚˜์”ฉ ์ž…๋ ฅํ•ด 3x3 ์‚ฌ๊ฐํ˜•์„ ๋งŒ๋“ค์–ด์„œ ์ฒดํฌํ•ด์ค€๋‹ค.

์ฒดํฌ ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์€ ๊ฐ„๋‹จํ•˜๋‹ค.
10๊ฐœ์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  ์ฃผ์–ด์ง„ 3x3์˜ ๋ฐฐ์—ด์—์„œ ๊ฐ™์€ ์ˆซ์ž๊ฐ€ ๋‚˜์˜ค๊ฑฐ๋‚˜ ํ˜น์€ 1๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ 9๋ณด๋‹ค ํฐ ์ˆซ์ž๊ฐ€ ๋‚˜์˜ค๋ฉด ๋‹น์—ฐํžˆ ์ œ์™ธํ•œ๋‹ค.
ํ†ต๊ณผํ•˜๋ฉด
ํ•˜๋‚˜์˜ ์„ ์„ ๋จผ์ € ๊ณ„์‚ฐํ•˜๊ณ  ๋‚˜๋จธ์ง€ ์„ ์ด ํ•ด๋‹น ์„ ์˜ ๊ฐ’๊ณผ ๊ฐ™์€์ง€ ๋น„๊ตํ•ด์ฃผ๋ฉด ๋!

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;
    }
};

image

๋‚˜๋ฆ„ ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ๋ฌธ์ œ ๋”ฑ ๋ด๋„ ๊ตฌํ˜„์ธ๋ฐ

ํ’€๋‹ค๊ฐ€ ์ƒท๊ฑด ์น ๋ป”ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ƒฅ ๋๊นŒ์ง€ ์žก์œผ๋ ค๋‹ค๊ฐ€ ํšจ์œจ ๊ฐœ๊ตฌ๋ฆฐ๊ฒƒ๊ฐ™์•„์„œ ๋ ˆํผ๋Ÿฐ์Šค ๋ณด๊ณ  ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

๋ด์ฃผ์„ธ์š”.

image
class Solution(object):
    def numMagicSquaresInside(self, grid):
        def magic(grid, r, c):
            expect = k * (k**2+1) // 2
            nums = set()
            min_num = float("inf")
            sum_diag, sum_anti = 0, 0
            for i in range(k):
                sum_diag += grid[r+i][c+i]
                sum_anti += grid[r+i][c+k-1-i]
                sum_r, sum_c = 0, 0
                for j in range(k):
                    min_num = min(min_num, grid[r+i][c+j])
                    nums.add(grid[r+i][c+j])
                    sum_r += grid[r+i][c+j]
                    sum_c += grid[r+j][c+i]
                if not (sum_r == sum_c == expect):
                    return False
            return sum_diag == sum_anti == expect and \
                len(nums) == k**2 and \
                min_num == 1

        k = 3
        result = 0
        for r in range(len(grid)-k+1):
            for c in range(len(grid[r])-k+1):
                if magic(grid, r, c):
                    result += 1
        return result

์ง„์งœ ์ƒท๊ฑด์น ๋ป”ํ–ˆ์–ด์š”

@yuyu0830
Copy link
Collaborator

์ด๊ฑฐ ํ˜น์‹œ ๋ˆ„์ ํ•ฉ์œผ๋กœ๋Š” ์•ˆ๋˜๋‚˜์š”? ํ•ด๋ด์•ผ์ง€

@InSange InSange merged commit 617e119 into main Aug 25, 2024
1 check passed
@InSange InSange deleted the 26-InSange branch August 25, 2024 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants