This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> ans;
int c;
bool ok(int rows, int cols, int grps){
int mxg = rows * cols / 4, mng = max(rows / 2, cols / 2);
return !(rows & 1 || cols & 1 || grps < mng || grps > mxg || grps == mxg - 1 || (rows == cols && grps == mng + 1));
}
void solve(int rows, int cols, int grps, int rind, int cind){
if (rows == 2){
for (int k = 0; k < cols; k += 2){
c++;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
ans[rind + i][cind + k + j] = c;
}
}
else if (cols == 2){
for (int k = 0; k < rows; k += 2){
c++;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
ans[rind + k + i][cind + j] = c;
}
}
else if (ok(rows - 2, cols - 2, grps - 1)){
c++;
for (int i = 0; i < rows; i++) ans[rind + i][cind] = ans[rind + i][cind + cols - 1] = c;
for (int i = 0; i < cols; i++) ans[rind][cind + i] = ans[rind + rows - 1][cind + i] = c;
solve(rows - 2, cols - 2, grps - 1, rind + 1, cind + 1);
}
else if (ok(rows, cols - 2, grps - rows / 2)){
for (int k = 0; k < rows; k += 2){
c++;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
ans[rind + k + i][cind + j] = c;
}
solve(rows, cols - 2, grps - rows / 2, rind, cind + 2);
}
else{
for (int k = 0; k < cols; k += 2){
c++;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
ans[rind + i][cind + k + j] = c;
}
solve(rows - 2, cols, grps - cols / 2, rind + 2, cind);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int cases; cin >> cases;
while (cases--){
int rows, cols, grps; cin >> rows >> cols >> grps;
if (!ok(rows, cols, grps)){
cout << "NO\n";
continue;
}
ans = vector<vector<int>>(rows, vector<int>(cols));
c = 0;
solve(rows, cols, grps, 0, 0);
cout << "YES\n";
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++) cout << ans[i][j] << ' ';
cout << '\n';
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |