๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

2630๋ฒˆ: ์ƒ‰์ข…์ด ๋งŒ๋“ค๊ธฐ

์ฒซ์งธ ์ค„์—๋Š” ์ „์ฒด ์ข…์ด์˜ ํ•œ ๋ณ€์˜ ๊ธธ์ด N์ด ์ฃผ์–ด์ ธ ์žˆ๋‹ค. N์€ 2, 4, 8, 16, 32, 64, 128 ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ์ƒ‰์ข…์ด์˜ ๊ฐ ๊ฐ€๋กœ์ค„์˜ ์ •์‚ฌ๊ฐํ˜•์นธ๋“ค์˜ ์ƒ‰์ด ์œ—์ค„๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ์ค„๊นŒ์ง€ ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

 

๐Ÿง‘๐Ÿป‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

๋งจ ์ฒ˜์Œ์—๋Š” \( N \times N \) ํฌ๊ธฐ์˜ ์ƒ‰์ข…์ด๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ์ฃผ์–ด์ง„ ์ •์‚ฌ๊ฐํ˜• ๋‚ด์˜ ์ƒ‰๊น”๋“ค์ด ๋ชจ๋‘ ๊ฐ™์€ ์ƒ‰๊น”์ธ์ง€๋ฅผ ๋ณผ ๊ฒ๋‹ˆ๋‹ค. ์ด ๋•Œ, ์„ ํƒ์ง€๋Š” ๋‘ ๊ฐ€์ง€๋กœ ๋‚˜๋‰˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

  1. ์ฃผ์–ด์ง„ ๋ฒ”์œ„ ๋‚ด์˜ ์ƒ‰๊น”์ด ๋ชจ๋‘ ๊ฐ™์€๊ฐ€?  \( \rightarrow \)  ํ•ด๋‹น ์ƒ‰๊น” ๊ฐœ์ˆ˜ +1ํ•˜๊ณ , ์ข…๋ฃŒ
  2. ์ƒ‰๊น”์ด ๊ฐ™์ง€ ์•Š์€๊ฐ€?  \( \rightarrow \)  ์ฃผ์–ด์ง„ ๋ฒ”์œ„์˜ ์‚ฌ๊ฐํ˜•์„ 4๋“ฑ๋ถ„ํ•˜์—ฌ ๋‹ค์‹œ ํƒ์ƒ‰ ์‹œ์ž‘

 

์ •์‚ฌ๊ฐํ˜• ํƒ์ƒ‰์€ ๋งจ ์™ผ์ชฝ ์œ„๋ถ€ํ„ฐ ์—ด ๋ฐฉํ–ฅ์œผ๋กœ ์‹œ์ž‘ํ•  ๊ฒƒ์ด๋ฉฐ, ์ด ์‹œ์ž‘ ์ง€์ ์˜ ํ–‰, ์—ด์„ (rowBegin, colBegin)์ด๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค.

ํ•ด๋‹น ์ •์‚ฌ๊ฐํ˜• ๋‚ด์˜ ์ƒ‰๊น”์ด ๋ชจ๋‘ ๊ฐ™์€ 1๋ฒˆ ์ผ€์ด์Šค์˜ ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ์ƒ‰๊น” ๊ฐœ์ˆ˜๋ฅผ +1 ํ•ด์ฃผ๊ณ  ๋๋‚ด๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

ํ•˜์ง€๋งŒ, 2๋ฒˆ ์ผ€์ด์Šค์— ํ•ด๋‹นํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” N์„ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆˆ ํ›„์— ๋‹ค์‹œ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

N์„ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆˆ ๋‹ค์Œ ํƒ์ƒ‰์—์„œ์˜ I, II, III, IV์˜ rowBegin, colBegin ๊ฐ’๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

(rowBegin, colBegin)                      // I
(rowBegin, colBegin + (N / 2))            // II
(rowBegin + (N / 2), colBegin)            // III
(rowBegin + (N / 2), colBegin + (N / 2))  // IV

 

์œ„์˜ ํ–‰, ์—ด์„ ์‹œ์ž‘์ ์œผ๋กœ ํ•˜์—ฌ ๊ฐ๊ฐ์˜ ์˜์—ญ์—์„œ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <iostream>
#include <vector>
#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
using namespace std;
using Square = vector<vector<int>>;
using Line = vector<int>;

void Search(const Square& coloredPaper, vector<int>& counts, int N, int rowBegin, int colBegin)
{
    if (N == 1)
    {
        int color = coloredPaper[rowBegin][colBegin];
        counts[color]++;
        return;
    }

    int color = -1;
    bool isAllEqaul = true;
    for(int row = rowBegin; row < rowBegin + N; row++)
    {
        for (int col = colBegin; col < colBegin + N; col++)
        {
            // ์ฒซ ๋ถ€๋ถ„ ์ƒ‰๊น” ์–ป๊ธฐ
            if (row == rowBegin && col == colBegin)
            {
                color = coloredPaper[row][col];
                continue;
            }

            // ์˜์—ญ ๋‚ด์— ์ƒ‰๊น”์ด ๋‹ค๋ฅธ ๊ฒŒ ์žˆ์„ ๊ฒฝ์šฐ, ๋ฐ˜๋ณต๋ฌธ ์ข…๋ฃŒ
            if (color != coloredPaper[row][col])
            {
                isAllEqaul = false;
                break;
            }
        }

        if (!isAllEqaul)
            break;
    }


    // ์˜์—ญ ๋‚ด์˜ ์ƒ‰๊น”์ด ๋ชจ๋‘ ๊ฐ™์„ ๊ฒฝ์šฐ, ํ•ด๋‹น ์ƒ‰๊น” ๊ฐœ์ˆ˜ +1
    if (color != -1 && isAllEqaul)
    {
        counts[color]++;
        return;
    }

    int next = N / 2;
    Search(coloredPaper, counts, next, rowBegin, colBegin);                 // I
    Search(coloredPaper, counts, next, rowBegin, colBegin + next);          // II
    Search(coloredPaper, counts, next, rowBegin + next, colBegin);          // III
    Search(coloredPaper, counts, next, rowBegin + next, colBegin + next);   // IV
}


int main()
{
    FAST_IO;

    int N;
    cin >> N;

    Square coloredPaper(N, Line(N));
    for (int i = 0; i < N; i++)
        for (int k = 0; k < N; k++)
            cin >> coloredPaper[i][k];

    vector<int> counts { 0, 0 };
    Search(coloredPaper, counts, N, 0, 0);

    for (const auto& color : counts)
        cout << color << "\n";
    return 0;
}

 

728x90
๋ฐ˜์‘ํ˜•