#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 (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) 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 (n == 2) {
if (s != m / 2) {
puts("NO");
continue;
}
init();
for (int i = 0; i < m / 2; i++) v[0][i * 2] = v[0][i * 2 + 1] = v[1][i * 2] = v[1][i * 2 + 1] = i;
print();
continue;
}
if (n == 4) {
int a = m - s;
int b = s - m / 2;
if (a < 0 || b < 0 || a == 1) {
puts("NO");
continue;
}
init();
if (a) {
v[0][0] = v[1][0] = v[2][0] = v[3][0] = 0;
v[0][2 * a - 1] = v[1][2 * a - 1] = v[2][2 * a - 1] = v[3][2 * a - 1] = 0;
for (int i = 0; i < a - 1; i++) {
v[0][i * 2 + 1] = v[0][i * 2 + 2] = v[3][i * 2 + 1] = v[3][i * 2 + 2] = 0;
v[1][i * 2 + 1] = v[1][i * 2 + 2] = v[2][i * 2 + 1] = v[2][i * 2 + 2] = i + 1;
}
}
for (int i = 0; i < b; i++) {
v[0][a * 2 + i * 2] = v[0][a * 2 + i * 2 + 1] = v[1][a * 2 + i * 2] = v[1][a * 2 + i * 2 + 1] = a + i * 2;
v[2][a * 2 + i * 2] = v[2][a * 2 + i * 2 + 1] = v[3][a * 2 + i * 2] = v[3][a * 2 + i * 2 + 1] = a + i * 2 + 1;
}
print();
continue;
}
if (solve(n / 2, m / 2, s)) continue;
puts("NO");
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:125:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
125 | for (scanf("%d", &t); t--; ) {
| ~~~~~^~~~~~~~~~
Main.cpp:126:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | scanf("%d%d%lld", &n, &m, &s);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
724 KB |
Correct! Azusa and Laika like the garden :) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
724 KB |
Correct! Azusa and Laika like the garden :) |
2 |
Correct |
13 ms |
624 KB |
Correct! Azusa and Laika like the garden :) |
3 |
Correct |
12 ms |
536 KB |
Correct! Azusa and Laika like the garden :) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
724 KB |
Correct! Azusa and Laika like the garden :) |
2 |
Correct |
13 ms |
624 KB |
Correct! Azusa and Laika like the garden :) |
3 |
Correct |
12 ms |
536 KB |
Correct! Azusa and Laika like the garden :) |
4 |
Failed |
13 ms |
596 KB |
Incorrect output |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
596 KB |
Correct! Azusa and Laika like the garden :) |
2 |
Correct |
13 ms |
580 KB |
Correct! Azusa and Laika like the garden :) |
3 |
Correct |
14 ms |
484 KB |
Correct! Azusa and Laika like the garden :) |
4 |
Correct |
12 ms |
712 KB |
Correct! Azusa and Laika like the garden :) |
5 |
Correct |
10 ms |
588 KB |
Correct! Azusa and Laika like the garden :) |
6 |
Correct |
11 ms |
580 KB |
Correct! Azusa and Laika like the garden :) |
7 |
Correct |
14 ms |
528 KB |
Correct! Azusa and Laika like the garden :) |
8 |
Correct |
11 ms |
468 KB |
Correct! Azusa and Laika like the garden :) |
9 |
Correct |
10 ms |
596 KB |
Correct! Azusa and Laika like the garden :) |
10 |
Correct |
10 ms |
596 KB |
Correct! Azusa and Laika like the garden :) |
11 |
Correct |
10 ms |
596 KB |
Correct! Azusa and Laika like the garden :) |
12 |
Correct |
10 ms |
596 KB |
Correct! Azusa and Laika like the garden :) |
13 |
Correct |
11 ms |
596 KB |
Correct! Azusa and Laika like the garden :) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Failed |
4 ms |
340 KB |
Incorrect output |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
724 KB |
Correct! Azusa and Laika like the garden :) |
2 |
Correct |
13 ms |
624 KB |
Correct! Azusa and Laika like the garden :) |
3 |
Correct |
12 ms |
536 KB |
Correct! Azusa and Laika like the garden :) |
4 |
Failed |
13 ms |
596 KB |
Incorrect output |
5 |
Halted |
0 ms |
0 KB |
- |