Mai intai trebuie sa te autentifici.

Cod sursa(job #251714)

Utilizator wefgefAndrei Grigorean wefgef Data 3 februarie 2009 10:26:57
Problema Tablete Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <cstdio>
#include <cassert>

const int MAX_N = 1024;

int n, k;
int a[MAX_N][MAX_N];
int cnt;

void fill(int c1, int c2) {
	for (int i = 1; i <= n; ++i)
		for (int j = c1; j <= c2; ++j)
			a[i][j] = ++cnt;
}

int main() {
	assert(freopen("tablete.in", "r", stdin) != NULL);
	assert(freopen("tablete.out", "w", stdout) != NULL);

	assert(scanf("%d %d", &n, &k) == 2);
	assert(3 <= n && n <= 1000);
	assert(1 < k && k < n);

	if (k % 2) {
		if (n % 2) {
			--n;
			fill(1, 1);
			fill(2, k);
			fill(k + 1, n);
			++n;

			for (int j = 1; j <= n; ++j)
				a[n][j] = n*n - (n-j) - 1;

			for (int i = 1; i + 1 < n; ++i)
				a[i][n] = ++cnt;
			a[n-1][n] = n * n;
		}
		else {
			fill(1, 1);
			fill(2, k);
			fill(k + 1, n);
		}
	} 
	else {
		fill(1, k);
		fill(k + 1, n);
	}

	printf("-123456789");
	for (int i = 1; i <= n; ++i, printf("\n"))
		for (int j = 1; j <= n; ++j)
			printf("%d ", a[i][j]);
}