Submission details
Task:Ruudukko
Sender:sharph2
Submission time:2025-09-27 10:48:12 +0300
Language:C++ (C++17)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED100
Test results
testverdicttime
#1ACCEPTED0.01 sdetails

Code

#include <algorithm>
#include <iostream>
#include <optional>
#include <random>
#include <vector>

using namespace std;

optional<vector<int>> ratko(int n, int m) {
    mt19937 rng(1237);

    if((min(n, m) == 1 && max(n, m) == 3) || max(n, m) == 2) {
        return nullopt;
    }

    vector<int> ret(n * m);
    for(int i = 0; i < (int)ret.size(); ++i) {
        ret[i] = i + 1;
    }

    bool ok = false;
    while(true) {
        shuffle(ret.begin(), ret.end(), rng);
        for(int i = 1; i < n; ++i) {
            for(int j = 0; j < m; ++j) {
                if(abs(ret[m * i + j] - ret[m * (i - 1) + j]) <= 1) {
                    goto nope;
                }
            }
        }
        for(int i = 0; i < n; ++i) {
            for(int j = 1; j < m; ++j) {
                if(abs(ret[m * i + j] - ret[m * i + (j - 1)]) <= 1) {
                    goto nope;
                }
            }
        }

        ok = true;
        break;

        nope: {}
    }

    if(ok) {
        return ret;
    } else {
        return nullopt;
    }
}

int main() {
    cin.sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    for(int ti = 0; ti < t; ++ti) {
        int n, m;
        cin >> n >> m;

        optional<vector<int>> retOpt = ratko(n, m);
        if(retOpt.has_value()) {
            vector<int>& ret = retOpt.value();
            int p = 0;
            cout << "YES\n";
            for(int i = 0; i < n; ++i) {
                for(int j = 0; j < m; ++j) {
                    cout << ret[p++] << " ";
                }
                cout << "\n";
            }
        } else {
            cout << "NO\n";
        }
    }

    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
100
1 1
1 2
2 1
1 3
...

correct output
YES
1
NO
NO
NO
...

user output
YES

NO
NO
NO
...