Cod sursa(job #2125817)

Utilizator sichetpaulSichet Paul sichetpaul Data 8 februarie 2018 19:12:54
Problema Piese Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <fstream>

using namespace std;
int n,m,sol,a[501][501];
int put(int x) {
   int p=1;
   while (p<=x)
      p*=2;
   return p/2;
}
void umple(int x,int y,int l,int c) {
    int i,j,p;
    p=put(min(l-x+1,c-y+1));
    ++sol;
    for (i=x;i<=x+p-1;++i)
    for (j=y;j<=y+p-1;++j)
        a[i][j]=sol;
    if (x+p<=l) umple(x+p,y,l,y+p-1);
    if (y+p<=c) umple(x,y+p,l,c);
}
int main()
{ int i,j;
    ifstream f("piese.in");
    ofstream g("piese.out");
    f>>n>>m;
    umple(1,1,n,m);
    g<<sol<<'\n';
    for (i=1;i<=n;++i) {
        for (j=1;j<=m;++j)
            g<<a[i][j]<<" ";
        g<<'\n';
    }
    return 0;
}