Cod sursa(job #1746239)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 22 august 2016 22:18:59
Problema Piese Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("piese.in");
ofstream out("piese.out");
const int maxn = 505;
int mat[maxn][maxn];

int get_pow(int x, int y)
{
    int p = 1;
    while(p * 2 <= min(x, y))
        p = p * 2;
    return p;
}

int N, M;
int cnt = 1;
void complete(int lin, int col, int n, int m)
{
    int p = get_pow(n - lin + 1, m - col + 1);
    for(int i = lin; i <= lin + p - 1; i++)
    {
        for(int j = col; j <= col + p - 1; j++)
        {
            mat[i][j] = cnt;
        }
    }
    if(lin > n || col > m)
        return;
    cnt++;
    complete(lin, col + p, lin + p - 1, m);
    complete(lin + p, col, n, m);
}

int main()
{
    in >> N >> M;
    int n = N;
    int m = M;
    complete(1, 1, n, m);
    out << cnt - 1 << "\n";
    for(int i = 1; i <= n; i++, out << "\n")
        for(int j = 1; j <= m; j++)
            out << mat[i][j] << " ";
    return 0;
}