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 <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int t, n, m;
long long s;
bool sw;
vector<vector<int>> v, u;
inline void init() {
v.clear();
v.resize(n);
for (int i = 0; i < n; i++) v[i].resize(m);
}
void print() {
puts("YES");
if (sw) {
swap(n, m);
u.clear();
u.resize(n);
for (int i = 0; i < n; i++) u[i].resize(m);
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) u[i][j] = v[j][i];
v = u;
}
for (auto &i: v) {
for (auto &j: i) printf("%d ", j + 1);
puts("");
}
}
bool solve(int x, int y, int z, int step = 0) {
if (x <= 0 || y <= 0) return false;
if (z == x * y) {
init();
for (int i = 0; i < step; i++) {
for (int j = i; j < m - i; j++) v[i][j] = v[n - 1 - i][j] = i;
for (int j = i; j < n - i; j++) v[j][i] = v[j][m - 1 - i] = i;
}
for (int i = 0; i < x; i++) for (int j = 0; j < y; j++) {
int curr = step + i * y + j;
v[step + i * 2][step + j * 2] = v[step + i * 2][step + j * 2 + 1] = v[step + i * 2 + 1][step + j * 2] = v[step + i * 2 + 1][step + j * 2 + 1] = curr;
}
print();
return true;
}
if (x <= 1 || y <= 1) return false;
if (z == x * y - 1) return false;
if (z <= x * y && z >= x * y - x) {
int cnt = x * y - z - 1;
init();
for (int i = 0; i < step; i++) {
for (int j = i; j < m - i; j++) v[i][j] = v[n - 1 - i][j] = i;
for (int j = i; j < n - i; j++) v[j][i] = v[j][m - 1 - i] = i;
}
v[step][step] = v[step][step + 1] = v[step][step + 2] = v[step][step + 3] = step;
v[step + cnt * 2 + 1][step] = v[step + cnt * 2 + 1][step + 1] = v[step + cnt * 2 + 1][step + 2] = v[step + cnt * 2 + 1][step + 3] = step;
for (int i = 0; i < cnt; i++) {
v[step + i * 2 + 1][step] = v[step + i * 2 + 1][step + 3] = step;
v[step + i * 2 + 2][step] = v[step + i * 2 + 2][step + 3] = step;
v[step + i * 2 + 1][step + 1] = v[step + i * 2 + 1][step + 2] = step + i + 1;
v[step + i * 2 + 2][step + 1] = v[step + i * 2 + 2][step + 2] = step + i + 1;
}
int k = step + cnt + 1;
for (int i = 0; i < x; i++) for (int j = 0; j < y; j++) {
if (i <= cnt && j <= 1) continue;
v[step + i * 2][step + j * 2] = v[step + i * 2][step + j * 2 + 1] = v[step + i * 2 + 1][step + j * 2] = v[step + i * 2 + 1][step + j * 2 + 1] = k++;
}
print();
return true;
}
if (z <= x * y && z >= x * y - x - y + 3) {
int cnt = x * y - z - x + 1;
init();
for (int i = 0; i < step; i++) {
for (int j = i; j < m - i; j++) v[i][j] = v[n - 1 - i][j] = i;
for (int j = i; j < n - i; j++) v[j][i] = v[j][m - 1 - i] = i;
}
for (int i = 0; i <= cnt; i++) v[step][step + i * 2] = v[step][step + i * 2 + 1] = step;
for (int i = 0; i <= cnt; i++) v[n - 1 - step][step + i * 2] = v[n - 1 - step][step + i * 2 + 1] = step;
int k = step + 1;
for (int i = 0; i < n / 2 - step - 1; i++) {
v[step + i * 2 + 1][step] = v[step + i * 2 + 1][step + cnt * 2 + 1] = step;
v[step + i * 2 + 2][step] = v[step + i * 2 + 2][step + cnt * 2 + 1] = step;
for (int j = step + 1; j < step + cnt * 2 + 1; j += 2) v[step + i * 2 + 1][j] = v[step + i * 2 + 2][j] = v[step + i * 2 + 1][j + 1] = v[step + i * 2 + 2][j + 1] = k++;
}
for (int i = 0; i < x; i++) for (int j = 0; j < y; j++) {
if (j <= cnt) continue;
v[step + i * 2][step + j * 2] = v[step + i * 2][step + j * 2 + 1] = v[step + i * 2 + 1][step + j * 2] = v[step + i * 2 + 1][step + j * 2 + 1] = k++;
}
print();
return true;
}
if (z == x * y - x - y + 1) {
int cnt = m / 2 - step - 2;
if (cnt < 2 || x < 3) return false;
init();
for (int i = 0; i < step; i++) {
for (int j = i; j < m - i; j++) v[i][j] = v[n - 1 - i][j] = i;
for (int j = i; j < n - i; j++) v[j][i] = v[j][m - 1 - i] = i;
}
for (int i = 0; i <= cnt; i++) v[step][step + i * 2] = v[step][step + i * 2 + 1] = step;
for (int i = 0; i <= cnt; i++) v[n - 1 - step][step + i * 2] = v[n - 1 - step][step + i * 2 + 1] = step;
v[step + 1][step + 1] = v[step + 1][step + 2] = v[step + 1][step + 3] = v[step + 1][step + 4] = step + 1;
v[step + 4][step + 1] = v[step + 4][step + 2] = v[step + 4][step + 3] = v[step + 4][step + 4] = step + 1;
v[step + 2][step + 1] = v[step + 2][step + 4] = v[step + 3][step + 1] = v[step + 3][step + 4] = step + 1;
v[step + 2][step + 2] = v[step + 2][step + 3] = v[step + 3][step + 2] = v[step + 3][step + 3] = step + 2;
int k = step + 3;
for (int i = 0; i < n / 2 - step - 1; i++) {
v[step + i * 2 + 1][step] = v[step + i * 2 + 1][step + cnt * 2 + 1] = step;
v[step + i * 2 + 2][step] = v[step + i * 2 + 2][step + cnt * 2 + 1] = step;
for (int j = step + 1 + (i < 2 ? 4 : 0); j < step + cnt * 2 + 1; j += 2) v[step + i * 2 + 1][j] = v[step + i * 2 + 2][j] = v[step + i * 2 + 1][j + 1] = v[step + i * 2 + 2][j + 1] = k++;
}
for (int i = 0; i < x; i++) for (int j = 0; j < y; j++) {
if (j <= cnt) continue;
v[step + i * 2][step + j * 2] = v[step + i * 2][step + j * 2 + 1] = v[step + i * 2 + 1][step + j * 2] = v[step + i * 2 + 1][step + j * 2 + 1] = k++;
}
print();
return true;
}
return solve(x - 1, y - 1, z - 1, step + 1);
}
int main() {
for (scanf("%d", &t); t--; ) {
scanf("%d%d%lld", &n, &m, &s);
sw = false;
if (n > m) swap(n, m), sw = true;
if (n % 2 || m % 2 || s > 200000) {
puts("NO");
continue;
}
if (solve(n / 2, m / 2, s)) continue;
puts("NO");
}
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:126:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | for (scanf("%d", &t); t--; ) {
| ~~~~~^~~~~~~~~~
Main.cpp:127:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | scanf("%d%d%lld", &n, &m, &s);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |