Pagini recente » Cod sursa (job #331338) | Cod sursa (job #890751) | Cod sursa (job #2105550) | Cod sursa (job #2457136) | Cod sursa (job #484650)
Cod sursa(job #484650)
#include<cstdio>
const int N=1002;
int n,k,a[N][N];
void afis()
{
for (int i=1;i<=n;++i)
{
for (int j=1;j<=n;++j)
printf("%d ",a[i][j]);
printf("\n");
}
}
int main()
{
freopen("tablete.in","r",stdin);
freopen("tablete.out","w",stdout);
scanf("%d%d",&n,&k);
if (k%2==0)
{
for (int i=1;i<=n;++i)
{
for (int j=1;j<=k;++j)
a[i][j]=(i-1)*k+j;
for (int j=k+1;j<=n;++j)
a[i][j]=n*k+(i-1)*(n-k)+(j-k);
}
}
else
{
for (int i=1;i<k;++i)
a[1][i]=i;
a[1][k]=k+1;
a[2][1]=k;
for (int i=2;i<=k;++i)
a[2][i]=k+i;
for (int i=3;i<=n;++i)
for (int j=1;j<=k;++j)
a[i][j]=a[i-2][j]+2*k;
if (n%2==1)
{
a[1][k+1]=k*n;
a[1][k+2]=k*n+2;
}
else
{
a[1][k+1]=k*n+1;
a[1][k+2]=k*n+2;
}
for (int i=k+3;i<=n;++i)
a[1][i]=a[1][i-1]+1;
a[2][k+1]=a[1][n]+1;
for (int i=k+2;i<=n;++i)
a[2][i]=a[2][i-1]+1;
a[3][k+1]=a[2][n]+1;
for (int i=k+2;i<=n;++i)
a[3][i]=a[3][i-1]+1;
for (int i=4;i<=n;++i)
for (int j=k+1;j<=n;++j)
a[i][j]=a[i-2][j]+(n-k)*2;
}
afis();
return 0;
}