Cod sursa(job #2494258)

Utilizator filiptudose2007Tudose Filip filiptudose2007 Data 17 noiembrie 2019 16:56:37
Problema Tablete Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <bits/stdc++.h>
using namespace std;
int a[1005][1005],n,k,l,c;
int main()
{
    freopen("tablete.in","r",stdin);
    freopen("tablete.out","w",stdout);
    cin.tie(0);
    cin.sync_with_stdio(0);
    cin>>n>>k;
    for(int i=1; i<=n; ++i)
    {
        if(l!=0)
        {
            a[i][1]=l;
            l=0;
        }
        else
        {
            c++;
            a[i][1]=c;
        }
        for(int j=2; j<k; ++j)
        {
            c++;
            a[i][j]=c;

        }
        if(c%2)
        {
            c++;
            a[i][k]=c;
        }
        else
        {
            c+=2;
            a[i][k]=c;
            l=c-1;
        }
        for(int j=k+1; j<=n; ++j)
        {
            ++c;
            a[i][j]=c;
        }
    }
    if(l!=0)
    {
        for(int j=n; j>1; --j)a[n][j]=a[n][j-1];
        a[n][1]=a[n-1][n];
        a[n-1][n]=l;
        if(a[n][1]>a[n][2])swap(a[n][1],a[n][2]);
    }
    for(int i=1; i<=n; ++i)
    {
        for(int j=1; j<=n; ++j)
            cout<<a[i][j]<<' ';
        cout<<'\n';
    }
    return 0;
}