Pagini recente » Cod sursa (job #216718) | Cod sursa (job #669368) | Cod sursa (job #887355) | Cod sursa (job #1491860) | Cod sursa (job #2112336)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("piese.in");
ofstream fout("piese.out");
int n, m, nrp, a[502][502];
int find_max_pow(int val)
{
int p=1;
while(p<=val) p*=2;
return p/2;
}
void umple(int x, int y, int lmax, int cmax)
{
int i, j, p;
p=find_max_pow(min(lmax-x+1,cmax-y+1));
++nrp;
for(i=x; i<=x+p-1; i++)
for(j=y; j<=y+p-1; j++)
a[i][j]=nrp;
if(x+p<=lmax) umple(x+p,y,lmax,y+p-1);
if(y+p<=cmax) umple(x,y+p,lmax,cmax);
}
int main()
{
int i, j;
fin>>n>>m;
umple(1,1,n,m);
fout<<nrp<<'\n';
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++) fout<<a[i][j]<<' ';
fout<<'\n';
}
return 0;
}