Cod sursa(job #303635)

Utilizator pedobearBacauanu Vlad pedobear Data 10 aprilie 2009 03:44:09
Problema Piese Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <stdio.h>

int tabla[512][512],put[12]={1,2,4,8,16,32,64,128,256,512,1024,2048};
int i,j,n,m,plac;

void acoperim(int x1,int y1,int x2,int y2)
{
     if (x1==x2 && y1==y2){
                tabla[x1][y1]=++plac;
                return;
                }
     int start=10;
     while ( (x2-x1+1)/put[start]<=0 || (y2-y1+1)/put[start]<=0) start--;
     plac++;
     for (i=x1;i<=x1+put[start]-1;i++)
         for (j=y1;j<=y1+put[start]-1;j++)
             tabla[i][j]=plac;
     if (x1+put[start]<=n && tabla[x1+put[start]][y1]==0 ) acoperim(x1+put[start],y1,x2,y2);
     if (y1+put[start]<=m && x1+put[start]-1<=n && tabla[x1][y1+put[start]]==0) acoperim(x1,y1+put[start],x1+put[start]-1,y2);
}

int main ()
{
    freopen ("piese.in","r",stdin);
    freopen ("piese.out","w",stdout);
    
    scanf ("%d %d",&n,&m);
    
    acoperim(1,1,n,m);
    
    printf ("%d\n",plac);
    for (i=1;i<=n;i++){
        for (j=1;j<=m;j++) printf ("%d ",tabla[i][j]);
        printf ("\n");
        }
    
    return 0;
}