Cod sursa(job #2254179)

Utilizator BlackLordFMI Alex Oprea BlackLord Data 4 octombrie 2018 20:49:03
Problema Tablete Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <fstream>
#include <iostream>

using namespace std;

ifstream fin ("tablete.in");
ofstream fout ("tablete.out");

int n, k, i, j, x, r, v[1000010];

int main () {
    fin >> n >> k;
    if (k % 2 == 0 && n % 2 == 0) {
        x = 1;
        for (i = 1; i <= n; ++i) {
            for (j = 1; j <= n; ++j) {
                fout << x++ << ' ';
            }
            fout << "\n";
        }
        return 0;
    }
    x = 1;
    for (i = 1; i <= n; ++i) {
        for (j = 1; j <= n; ++j) {
            if (j == 1 && r != 0) {
                v[r] = 1;
                fout << r << ' ';
                r = 0;
                continue;
            } else {
                if (j == k) {
                    if (x % 2 == 0) {
                        fout << x << ' ';
                        v[x] = 1;
                    } else {
                        fout << x + 1 << ' ';
                        v[x + 1] = 1;
                        r = x++;
                    }
                } else {
                    while (v[x] == 1) {
                        x++;
                    }
                    fout << x << ' ';
                }
                x++;
            }
        }
        fout << "\n";
    }
}