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

1992๋ฒˆ: ์ฟผ๋“œํŠธ๋ฆฌ

์ฒซ์งธ ์ค„์—๋Š” ์˜์ƒ์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž N ์ด ์ฃผ์–ด์ง„๋‹ค. N ์€ ์–ธ์ œ๋‚˜ 2์˜ ์ œ๊ณฑ์ˆ˜๋กœ ์ฃผ์–ด์ง€๋ฉฐ, 1 ≤ N ≤ 64์˜ ๋ฒ”์œ„๋ฅผ ๊ฐ€์ง„๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ๋Š” ๊ธธ์ด N์˜ ๋ฌธ์ž์—ด์ด N๊ฐœ ๋“ค์–ด์˜จ๋‹ค. ๊ฐ ๋ฌธ์ž์—ด์€ 0 ๋˜

www.acmicpc.net

 

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

๐Ÿ”—2630๋ฒˆ ๋ฌธ์ œ์™€ ์œ ์‚ฌํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ „์ฒด 2์ฐจ์› ๋ฐฐ์—ด์„ image๋ผ๊ณ  ํ•˜๊ณ , ํ˜„์žฌ ํƒ์ƒ‰ ์ค‘์ธ ์‚ฌ๊ฐํ˜• ์˜์—ญ์˜ ์ œ์ผ ์™ผ์ชฝ ์œ„ ์‹œ์ž‘์ ์„ (rowBegin, colBegin)์ด๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

  1. \(N \times N \) ํฌ๊ธฐ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ Top-down์œผ๋กœ ๋‚ด๋ ค ๊ฐ„๋‹ค.
  2. N == 1์ด๋ผ๋ฉด, ์ •๋‹ต ๋ฌธ์ž์—ด์— ํ˜„์žฌ ์ƒ‰๊น”(image[rowBegin][colBegin])์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.
  3. N != 1์ด๋ผ๋ฉด, ํ˜„์žฌ ์‚ฌ๊ฐํ˜• ์˜์—ญ์ด ๋ชจ๋‘ ๊ฐ™์€ ์ƒ‰๊น”์ธ์ง€ ํŒŒ์•…ํ•˜์—ฌ ์••์ถ•์ด ๊ฐ€๋Šฅํ•œ์ง€ ์•Œ์•„๋ณธ๋‹ค.
    • ์••์ถ•์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด, ์ •๋‹ต ๋ฌธ์ž์—ด์— ํ˜„์žฌ ์‚ฌ๊ฐํ˜• ์˜์—ญ์˜ ์ƒ‰๊น”์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.
  4. ํ˜„์žฌ ์‚ฌ๊ฐํ˜• ์˜์—ญ์ด ๋ชจ๋‘ ๊ฐ™์€ ์ƒ‰์ด ์•„๋‹ˆ๋ผ๋ฉด, ์ •๋‹ต ๋ฌธ์ž์—ด์— '('์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
  5. N = N / 2๋ฅผ ์ ์šฉํ•œ๋‹ค.
  6. ํ˜„์žฌ ์‚ฌ๊ฐํ˜•์„ ๋‹ค์‹œ 4๋“ฑ๋ถ„ํ•˜์—ฌ, rowBegin๊ณผ colBegin์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐฑ์‹ ํ•˜์—ฌ ์žฌ๊ท€๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค.
    • ์™ผ์ชฝ ์œ„ : (rowBegin, colBegin)
    • ์˜ค๋ฅธ์ชฝ ์œ„ : (rowBegin, colBegin + N)
    • ์™ผ์ชฝ ์•„๋ž˜ : (rowBegin + N, colBegin)
    • ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ : (rowBegin + N, colBegin + N)
  7. ์ •๋‹ต ๋ฌธ์ž์—ด์— ')'์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

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

#include <iostream>
#include <vector>
#include <string>
#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
using namespace std;
const char UNCOMPRESSIBLE = '?';

char IsAvailableCompress(const vector<string>& image, int N, int rowBegin, int colBegin)
{
    char color = '?';

    for (int row = rowBegin; row < rowBegin + N; row++)
    {
        for (int col = colBegin; col < colBegin + N; col++)
        {
            if (row == rowBegin and col == colBegin)
            {
                color = image[row][col];
                continue;
            }

            if (image[row][col] != color)
                return UNCOMPRESSIBLE;
        }
    }

    return color;
}

void Compress(const vector<string>& image, string& result, int N, int rowBegin, int colBegin)
{
    if (N == 1)
    {
        result += image[rowBegin][colBegin];
        return;
    }

    char flag = IsAvailableCompress(image, N, rowBegin, colBegin);
    
    if (flag != UNCOMPRESSIBLE)
    {
        result += flag;
        return;
    }

    result.push_back('(');
    N /= 2;

    Compress(image, result, N, rowBegin, colBegin);             // ์™ผ์ชฝ ์œ„
    Compress(image, result, N, rowBegin, colBegin + N);         // ์˜ค๋ฅธ์ชฝ ์œ„
    Compress(image, result, N, rowBegin + N, colBegin);         // ์™ผ์ชฝ ์•„๋ž˜
    Compress(image, result, N, rowBegin + N, colBegin + N);     // ์˜ค๋ฅธ์ชฝ ์•„๋ž˜

    result.push_back(')');
}


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

    vector<string> bwImage(N);
    for (int i = 0; i < N; i++)
        cin >> bwImage[i];


    string answer = "";
    Compress(bwImage, answer, N, 0, 0);

    cout << answer;
    return 0;
}

 

 

728x90
๋ฐ˜์‘ํ˜•