Cod sursa(job #2595996)

Utilizator razvan123vRazvan Vranceanu razvan123v Data 8 aprilie 2020 22:00:30
Problema Tablete Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.55 kb
//
//  main.cpp
//  tablete
//  Created by Razvan Vranceanu on 08/04/2020.
//

#include <iostream>
#include <fstream>
#define NMAX 1006
using namespace std;
ifstream f("tablete.in");
ofstream g("tablete.out");

int a[NMAX][NMAX], nr, n, k;
bool folosit[NMAX];

void completare_stanga(int i)
{
    a[i][k] = i * k + 1;
    for (int j = k - 1; j >= 1; j--)
        a[i][j] = 2 * k * ((i + 1) / 2 - 1) + j;

    a[i + 1][1] = i * k;
    a[i + 1][2] = i * k + 2;
    for (int j = 3; j <= k; j++)
        a[i + 1][j] = a[i + 1][j - 1] + 1;
}

//completare casute libere dreapta
void completare_dreapta()
{
    nr = n * n;
    for (int i = n; i >= 1; i--)
        for (int j = n; j > k; j--)
            a[i][j] = nr--;
}

int main()
{
    f >> n >> k;
    if (k % 2 == 0)
    {
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                a[i][j] = ++nr;
    }
    else if (n % 2 == 0 && k % 2 == 1)
    {
        for (int i = 1; i <= n; i += 2)
            completare_stanga(i);

        completare_dreapta();
    }
    else if (n % 2 && k % 2)
    {
        for (int i = 1; i <= n - 1; i += 2)
            completare_stanga(i);
        //ultima linie
        a[n][1] = ((n - 1) * k) + 1;
        for (int j = 2; j < k; j++)
            a[n][j] = a[n][j - 1] + 1;
        a[n][k] = a[n][k - 1] + 2;

        completare_dreapta();
        
        a[1][k]=n*k;
    }
    //afisare
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
            g << a[i][j] << " ";
        g << endl;
    }
    f.close();
    g.close();
    return 0;
}