[BOJ] 1780번 | μ’…μ΄μ˜ 개수 (C++)

2023. 8. 31. 22:38ㆍCoding Test/BOJ

πŸ”—λ¬Έμ œ λ³΄λŸ¬κ°€κΈ°
 

1780번: μ’…μ΄μ˜ 개수

N×N크기의 ν–‰λ ¬λ‘œ ν‘œν˜„λ˜λŠ” 쒅이가 μžˆλ‹€. μ’…μ΄μ˜ 각 μΉΈμ—λŠ” -1, 0, 1 쀑 ν•˜λ‚˜κ°€ μ €μž₯λ˜μ–΄ μžˆλ‹€. μš°λ¦¬λŠ” 이 행렬을 λ‹€μŒκ³Ό 같은 κ·œμΉ™μ— 따라 μ μ ˆν•œ 크기둜 자λ₯΄λ €κ³  ν•œλ‹€. λ§Œμ•½ 쒅이가 λͺ¨λ‘ 같은 수

www.acmicpc.net

 

πŸ§‘πŸ»‍πŸ’»ν’€μ΄ κ³Όμ •

πŸ”—1992번 λ¬Έμ œμ™€ λΉ„μŠ·ν•œλ°, 4개의 λΆ„ν• μ—μ„œ 9개둜 λŠ˜μ—ˆλ‹€λŠ” 점만 λ‹€λ₯Έ 것을 λΉΌλ©΄ ν’€μ΄λŠ” λ˜‘κ°™μŠ΅λ‹ˆλ‹€.

μ •λ§λ‘œ λ˜‘κ°™μ•„μ„œ, λ”°λ‘œ 적을 말이 μ—†λ„€μš”...

 

βœοΈμ†ŒμŠ€ μ½”λ“œ 및 κ²°κ³Ό

#include <iostream>
#include <vector>
#include <map>
#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
using namespace std;
const int ALL_NOT_SAME = -2;


int FindNumberInSquare(const vector<vector<int>>& square, int N, int rowBegin, int colBegin)
{
    int number = ALL_NOT_SAME;
    for (int row = rowBegin; row < rowBegin + N; row++)
    {
        for (int col = colBegin; col < colBegin + N; col++)
        {
            if (row == rowBegin and col == colBegin)
            {
                number = square[row][col];
                continue;
            }

            if (square[row][col] != number)
                return ALL_NOT_SAME;
        }
    }

    return number;
}

void Solution(const vector<vector<int>>& paper, map<int, int>& counts, int N, int rowBegin, int colBegin)
{
    if (N == 1)
    {
        int number = paper[rowBegin][colBegin];
        counts[number]++;
        return;
    }

    int number = FindNumberInSquare(paper, N, rowBegin, colBegin);
    if (number != ALL_NOT_SAME)
    {
        counts[number]++;
        return;
    }

    N /= 3;

    for (int row = 0; row < 3; row++)
        for (int col = 0; col < 3; col++)
            Solution(paper, counts, N, rowBegin + N * row, colBegin + N * col);
}

int main()
{
    FAST_IO
    int N;
    cin >> N;

    vector<vector<int>> paper(N, vector<int>(N));
    for (int row = 0; row < N; row++)
        for(int col = 0; col < N; col++)
            cin >> paper[row][col];

    map<int, int> answers
    {
        {-1, 0},
        {0,  0},
        {1,  0}
    };


    Solution(paper, answers, N, 0, 0);
    for (const auto& answer : answers)
        cout << answer.second << "\n";
    return 0;
}

 

μž¬κ·€ ν•¨μˆ˜λ₯Ό 9개 μ¨λ†¨λ˜ κ±Έ, 2쀑 for문으둜 λ°”κΎΈμ—ˆλŠ”λ° μ™œ μ‹œκ°„μ΄ 더 λŠ˜μ—ˆμ„κΉŒμš”...

 

728x90
λ°˜μ‘ν˜•