Pagini recente » Cod sursa (job #3229966) | Cod sursa (job #406084) | Cod sursa (job #694849) | Cod sursa (job #1114852) | Cod sursa (job #227008)
Cod sursa(job #227008)
Utilizator |
Stefan Istrate stef2n |
Data |
3 decembrie 2008 13:14:57 |
Problema |
Tablete |
Scor |
Ascuns |
Compilator |
cpp |
Status |
done |
Runda |
|
Marime |
1.77 kb |
#include <cstdio>
#include <cassert>
const int MAX_N = 1005;
int N, K;
int A[MAX_N][MAX_N];
int main() {
freopen("tablete.in", "r", stdin);
freopen("tablete.out", "w", stdout);
scanf("%d %d", &N, &K);
assert(3 <= N && N <= 1000);
assert(1 < K && K < N);
int cnt = 1;
if(K % 2 == 0) {
for(int i = 1; i <= N; ++i)
for(int j = 1; j <= K; ++j)
A[i][j] = cnt++;
for(int i = 1; i <= N; ++i)
for(int j = K + 1; j <= N; ++j)
A[i][j] = cnt++;
}
else
if(N % 2 == 0) {
for(int i = 1; i <= N; i += 2) {
for(int j = 1; j < K; ++j)
A[i][j] = cnt++;
A[i + 1][1] = cnt++;
A[i][K] = cnt++;
for(int j = 2; j <= K; ++j)
A[i + 1][j] = cnt++;
}
for(int i = 1; i <= N; ++i)
for(int j = K + 1; j <= N; ++j)
A[i][j] = cnt++;
}
else {
for(int i = 1; i < N; i += 2) {
for(int j = 1; j < K; ++j)
A[i][j] = cnt++;
A[i + 1][1] = cnt++;
A[i][K] = cnt++;
for(int j = 2; j <= K; ++j)
A[i + 1][j] = cnt++;
}
for(int j = 1; j < K; ++j)
A[N][j] = cnt++;
A[1][K + 1] = cnt++;
A[N][K] = cnt++;
for(int j = K + 2; j <= N; ++j)
A[1][j] = cnt++;
for(int i = 2; i <= N; ++i)
for(int j = K + 1; j <= N; ++j)
A[i][j] = cnt++;
}
for(int i = 1; i <= N; ++i) {
for(int j = 1; j <= N; ++j)
printf("%d ", A[i][j]);
printf("\n");
}
return 0;
}