2630 - 색종이 만들기

Solution

code

#include <iostream>
#include <utility>
using namespace std;

bool sameColor(int ** arr, int left, int top, int div, int N)
{
    bool comp = arr[top][left]; 
    for(int i = top; i < N/div + top; i++)
    {
        for(int j = left; j < N/div + left; j++)
        {
            if(arr[i][j] != comp) return false;
        }
    }
    return true;
}

pair<int,int> count(int ** arr, int left, int top, int div, int N)
{
    if( sameColor(arr, left, top, div, N) )
    {
        if (arr[top][left] == 1) return make_pair(0, 1);
        else return make_pair(1, 0);
    }
    else
    {
        div *= 2;
        pair<int, int> lt = count(arr, left, top, div, N);
        pair<int, int> rt = count(arr, left + (N/div), top, div, N);
        pair<int, int> lb = count(arr, left, top + (N/div), div, N);
        pair<int, int> rb = count(arr, left + (N/div), top + (N/div), div, N);
        return make_pair((lt.first + rt.first + lb.first + rb.first), (lt.second + rt.second + lb.second + rb.second));
    }
}

int main()
{
    int N;
    cin >> N;
    int ** arr = new int*[N];
    for(int i = 0; i < N; i++)
    {
        arr[i] = new int[N];
        for(int j = 0; j < N; j++)
        {
            cin >> arr[i][j];
        }
    }

    pair<int, int> print = count(arr, 0, 0, 1, N);
    cout << print.first << " " << print.second << endl;
    return 0;
}
#include <iostream>
using namespace std;

int main()
{
    int N;
    cin >> N;
    int ** array = new int*[N];

    /*
        < 2 - dimentional array, index : [i][j] >
        --> direction of j (row)
        *---*---*---*---*  | direction of i (column)
        |0,0|0,1|0,2|0,3|  v
        *---*---*---*---*
        |1,0|1,1|1,2|1,3|
        *---*---*---*---*
        |2,0|2,1|2,2|2,3|
        *---*---*---*---*
        |3,0|3,1|3,2|3,3|
        *---*---*---*---*
    */

    // dynamic allocation of 2-dimentional array(size N*N)
    for(int i = 0; i < N; i++)
    {
        // dynamic allocation of row
        array[i] = new int[N];
        for(int j = 0; j < N; j++)
            cin >> array[i][j];
    }
    cout << "==================\n";

    // print allocated array
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
            cout << array[i][j] << " ";
        cout << endl;
    }
    cout << "==================\n";

    // print each element
    cout << "array[2][2] : " << array[2][2] << " == (2*N) + 2 == (i*N) + j" << endl;
    cout << "array[2][3] : " << array[2][3] << " == (2*N) + 3 == (i*N) + j" << endl;
    cout << "array[3][2] : " << array[3][2] << " == (3*N) + 2 == (i*N) + j" << endl;
    cout << "array[7][7] : " << array[7][7] << " == (7*N) + 7 == (i*N) + j" << endl;

    return 0;    
}

ref

2630번: 색종이 만들기