Cod sursa(job #3334919)

Utilizator dio2011Tudor Dionisie dio2011 Data 20 ianuarie 2026 17:47:20
Problema Tablete Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("tablete.in");
ofstream cout("tablete.out");
int a[1005][1005];
int main()
{
    int n, k;
    cin >> n >> k;
    if(k%2==0)
    {
        for(int x=1;x<=n;x++)
        {
            for(int j=1;j<=n;j++)
            {
                a[x][j]=n*x+j;
            }
        }
    }
    else
    {
        if(n%2==0)
        {
            for(int x=1;x<n;x++)
            {
                a[1][x]=x+1;
            }
            a[1][n]=2*n;
            a[2][1]=1;
            for(int x=2;x<n;x++)
            {
                a[2][x]=a[1][x]+n-2;
            }
            a[2][n]=2*n-1;
            for(int x=3;x<=n;x++)
            {
                for(int j=1;j<=n;j++)
                {
                    a[x][j]=a[x-2][j]+2*n;
                }
            }
        }
        else
        {
            for(int x=1;x<=n;x++)
            {
                a[1][x]=x+1;
            }
            a[2][1]=1;
            for(int x=2;x<=n;x++)
            {
                a[2][x]=a[1][x]+n-1;
            }
            for(int x=3;x<=n-2;x++)
            {
                for(int j=1;j<=n;j++)
                {
                    a[x][j]=a[x-2][j]+2*n;
                }
            }
            for(int j=1;j<n;j++)
            {
                a[n-1][j]=a[n-3][j]+2*n;
            }
            a[n-1][n]=n*n;
            for(int j=1;j<=n;j++)
            {
                a[n][j]=n*(n-1)+j-1;
            }
        }
    }
    for(int x=1;x<=n;x++)
    {
        for(int j=1;j<=n;j++)
        {
            cout << a[x][j] << " ";
        }
        cout << '\n';
    }
    return 0;
}