Cod sursa(job #1688114)

Utilizator vladvlad00Vlad Teodorescu vladvlad00 Data 13 aprilie 2016 11:44:10
Problema Piese Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <fstream>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

ifstream fin("piese.in");
ofstream fout("piese.out");

struct piesa
{
	int x, y, l;
};

int latura(int x, int y);

int n, m, terminat, nrsol , in[505], a[505][505];
piesa sol[505];

int main()
{
	int i, j;
	
	fin >> n >> m;
	for (i = 1; i <= n; i++)
		in[i] = 1;
	while (!terminat)
	{
		terminat = 1;
		i = 1;
		while (i<=n)
			if (in[i] <= m)
			{
				terminat = 0;
				nrsol++;
				sol[nrsol].x = i;
				sol[nrsol].y = in[i];
				sol[nrsol].l = latura(i, in[i]);
				for (; i <= sol[nrsol].x + sol[nrsol].l - 1; i++)
					in[i] = in[i] + sol[nrsol].l;
			}
			else i++;
	}
	fout << nrsol << '\n';
	while (nrsol)
	{
		for (i = sol[nrsol].x; i <= sol[nrsol].x + sol[nrsol].l - 1; i++)
			for (j = sol[nrsol].y; j <= sol[nrsol].y + sol[nrsol].l - 1; j++)
				a[i][j] = nrsol;
		nrsol--;
	}
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
			fout << a[i][j] << ' ';
		fout << '\n';
	}
	return 0;
}

int latura(int x, int y)
{
	int i = 1;

	while (x + i - 1 <= n&&y + i - 1 <= m)
		i <<= 1;
	return i>>1;
}