Submission details
Task:Ruudukko
Sender:Kuha
Submission time:2025-09-27 16:52:38 +0300
Language:C++ (C++11)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED100
Test results
testverdicttime
#1ACCEPTED0.01 sdetails

Code

#include <bits/stdc++.h>

using namespace std;

int main() {
    srand(time(0));
    int t;
    cin>>t;
    while (t --> 0) {
        int n, m;
        cin>>n>>m;
        int v[n][m];
        int p[n * m];
        for (int i = 0; i < n * m; i++) p[i] = i + 1;
        random_shuffle(p, p + (n * m));
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                v[i][j] = p[i * m + j];
            }
        }
        for (int x = 0; x < 1000; x++) {
            int ok = true;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (i && abs(v[i][j] - v[i - 1][j]) == 1) {
                        swap(v[i][j], v[rand() % n][rand() % m]);
                        ok = false;
                    }
                    if (j && abs(v[i][j] - v[i][j - 1]) == 1) {
                        swap(v[i][j], v[rand() % n][rand() % m]);
                        ok = false;
                    }
                }
            }
            if (ok) {
                cout<<"YES"<<endl;
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < m; j++) {
                        cout<<v[i][j]<<" ";
                    }
                    cout<<endl;
                }
                goto end;
            }
        }
        cout<<"NO"<<endl;
        end:;
    }
}

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
...