Submission #1038814

#TimeUsernameProblemLanguageResultExecution timeMemory
1038814andrei_c1Gardening (RMI21_gardening)C++17
100 / 100
172 ms1876 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; int tc, n, m, k, col; vector<vector<int>> v; map<ll, bool> can; ll kN = 2e5; #define hash sateiadracu ll hash(int n, int m, int k) { return kN * kN * n + kN * m + k; } bool check(int n, int m, int k) { if(n > m) { swap(n, m); } ll h = hash(n, m, k); return can.find(h) != can.end() && can[h]; } bool solve(int n, int m, int k) { if(n % 2 || m % 2) { return 0; } if(k > n * m / 4) { return 0; } if(n > m) { swap(n, m); } if(k < m / 2) { return 0; } ll h = hash(n, m, k); if(can.find(h) != can.end()) { return can[h]; } if(k == 1) { return can[h] = (n == 2 && m == 2); } // split col for(int k1 = 1; k1 < k; k1++) { for(int j = 2; j < m - 1; j += 2) { if(solve(n, j, k1) && solve(n, m - j, k - k1)) { return can[h] = true; } } } // slpit border if(solve(n - 2, m - 2, k - 1)) { return can[h] = true; } return can[h] = false; } void rec(int a, int b, int c, int d, int k) { int n = c - a + 1, m = d - b + 1; if(n == 1 || m == 1) { return; } if(k == 1) { if(n == 2 && m == 2) { v[a][b] = v[a][d] = v[c][b] = v[c][d] = col; col++; } return; } if(check(c - a - 1, d - b - 1, k - 1)) { for(int i = b; i <= d; i++) { v[a][i] = v[c][i] = col; } for(int i = a; i <= c; i++) { v[i][b] = v[i][d] = col; } col++; rec(a + 1, b + 1, c - 1, d - 1, k - 1); return; } for(int j = b + 1; j < d; j++) { for(int k1 = 1; k1 < k; k1++) { if(check(c - a + 1, j - b + 1, k1) && check(c - a + 1, d - j, k - k1)) { rec(a, b, c, j, k1); rec(a, j + 1, c, d, k - k1); return; } } } for(int j = a + 1; j < c; j++) { for(int k1 = 1; k1 < k; k1++) { if(check(j - a + 1, d - b + 1, k1) && check(c - j, d - b + 1, k - k1)) { rec(a, b, j, d, k1); rec(j + 1, b, c, d, k - k1); return; } } } } void solve() { cin >> n >> m >> k; can.clear(); bool ok = solve(n, m, k); if(!ok) { cout << "NO\n"; return; } cout << "YES\n"; v = vector<vector<int>>(n + 1, vector<int>(m + 1)); col = 1; rec(1, 1, n, m, k); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { cout << v[i][j] << " "; } cout << "\n"; } } int main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> tc; while(tc--) { solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...