Pagini recente » Cod sursa (job #74468) | Cod sursa (job #2169733) | Cod sursa (job #2004734) | Cod sursa (job #2912691) | Cod sursa (job #227961)
Cod sursa(job #227961)
Utilizator |
Stefan Istrate stef2n |
Data |
5 decembrie 2008 23:08:44 |
Problema |
Tablete |
Scor |
Ascuns |
Compilator |
cpp |
Status |
done |
Runda |
|
Marime |
1.8 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");
}
printf(" ");
return 0;
}