結果

問題 No.3734 No Flat Notes
コンテスト
ユーザー ponjuice
提出日時 2026-09-19 15:28:33
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 7,103 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,483 ms
コンパイル使用メモリ 356,780 KB
実行使用メモリ 9,924 KB
最終ジャッジ日時 2026-09-19 15:28:42
合計ジャッジ時間 5,401 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
サブタスク 配点 結果
部分点 20 % WA * 28
満点 80 % AC * 1 WA * 59
合計 3.5 * 0% = 0 点
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

//高速化 
struct ponjuice{ponjuice(){cin.tie(0);ios::sync_with_stdio(0);cout<<fixed<<setprecision(20);}}PonJuice;
#define endl '\n' //インタラクティブ問題の時は消す

//型
using ll = long long;
using ld = long double;

// for文
#define overload4(a, b, c, d, e, ...) e
#define rep1(n)             for(ll i = 0; i < n; i++)
#define rep2(i, n)          for(ll i = 0; i < n; i++)
#define rep3(i, a, b)       for(ll i = a; i < b; i++)
#define rep4(i, a, b, step) for(ll i = a; i < b; i+= step)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define per1(n)             for(ll i = n-1; i >= 0; i--)
#define per2(i, n)          for(ll i = n-1; i >= 0; i--)
#define per3(i, a, b)       for(ll i = b-1; i >= a; i--)
#define per4(i, a, b, step) for(ll i = b-1; i >= a; i-= step)
#define per(...) overload4(__VA_ARGS__, per4, per3, per2, per1)(__VA_ARGS__)

//関数
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class S, class T>inline bool chmax(S& a, T b){return a < b && ( a = b , true);}
template<class S, class T>inline bool chmin(S& a, T b){return a > b && ( a = b , true);}

//定数
constexpr ll mod = 998244353;
constexpr ll minf=-(1<<29);
constexpr ll inf=(1<<29);
constexpr ll MINF=-(1LL<<60);
constexpr ll INF=(1LL<<60);
const int dx[4] ={1, 0, -1, 0};
const int dy[4] ={ 0, 1, 0,-1};
const int dx8[8] ={-1,-1,-1, 0, 1, 1, 1, 0};
const int dy8[8] ={-1, 0, 1, 1, 1, 0,-1,-1};

void solve();
int main() {
	int t = 1;
    cin >> t;
    while(t--) {
        solve();
    }
}

void solve(){
    int h,w,m;
    cin >> h >> w >> m;

    if(m == 0) {
        cout << -1 << endl;
        return;
    }
    bool sw = false;
    if(h > w) {
        sw = true;
        swap(h, w);
    }
    vector<vector<int>> ans(h, vector<int>(w, 0));

    if(h == 1) {
        rep(i,0,w+1-m*2) {
            ans[0][i] = i+1;
        }
        ans[0][w+1-m*2] = w;
        rep(i,0,m-1) {
            ans[0][w+1-m*2+1+i*2] = w-2-i*2;
            ans[0][w+1-m*2+2+i*2] = w-1-i*2;
        }
    }else if(h*w % 2 == 0){
        bool sw2 = false;
        if(w % 2 == 1) {
            sw2 = true;
            swap(h,w);
            ans = vector<vector<int>>(h, vector<int>(w, 0));
        }

        vector<array<ll,4>> ps;
        rep(i,0,h-2) {
            rep(j,0,w/2) {
                ps.push_back({i,j*2, i,j*2+1});
            }
        }
        rep(j,0,w) {
            ps.push_back({h-2, j, h-1, j});
        }
        set<int> p;
        rep(i,0,h*w) p.insert(i+1);

        rep(i,0,m) {
            auto [p1x,p1y, p2x, p2y] = ps[i];
            if((p1x+p1y)%2 == 1) {
                swap(p1x, p2x);
                swap(p1y, p2y);
            }
            ans[p1x][p1y] = *p.begin();
            ans[p2x][p2y] = *prev(p.end());
            p.erase(p.begin());
            p.erase(prev(p.end()));
        }

        bool rev = false;
        auto dfs = [&](int sx, int sy) {
            // した右上左の順で埋める
            int x = sx, y = sy;
            while(true) {
                if(ans[x][y]) break;
                if(rev) {
                    ans[x][y] = *prev(p.end());
                    p.erase(prev(p.end()));
                }else {
                    ans[x][y] = *p.begin();
                    p.erase(p.begin());
                }

                rep(i,0,4) {
                    int nx = x + dx[i], ny = y + dy[i];
                    if(nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
                    if(ans[nx][ny] != 0) continue;
                    x = nx, y = ny;
                    break;
                }
            }
        };
        rep(i,0,h) {
            if(ans[i][0] == 0) {
                if((i+0) % 2 == 0) rev = true;
                dfs(i, 0);
                break;
            }
        }
        rep(i,0,w) {
            if(ans[h-1][i] == 0) {
                if((h-1+i) % 2 == 0) rev = true;
                dfs(h-1, i);
                break;
            }
        }

        if(sw2) {
            vector<vector<int>> res(w, vector<int>(h, 0));
            rep(i,0,h){
                rep(j,0,w) {
                    res[j][i] = ans[i][j];
                }
            }
            ans.swap(res);
        }
    }else {
        cout << -1 << endl;
        return;
        if(m*2+1 == h*w) {
            cout << -1 << endl;
            return;
        }
        vector<array<ll,4>> ps;
        rep(i,0,(h-1)/2) {
            ps.push_back({1+i*2, 0, 2+i*2, 0});
        }
        rep(i,0,h-2) {
            rep(j,0,(w-3)/2) {
                ps.push_back({i,3+j*2, i,3+j*2+1});
            }
        }
        per(j,3,w) {
            ps.push_back({h-2, j, h-1, j});
        }
        per(i,0,h) {
            ps.push_back({i,1, i, 2});
        }
        set<int> p;
        rep(i,0,h*w) p.insert(i+1);

        p.erase(h*w-m-1);
        ans[0][0] = h*w-m-1;

        rep(i,0,m) {
            auto [p1x,p1y, p2x, p2y] = ps[i];
            if((p1x+p1y)%2 == 1) {
                swap(p1x, p2x);
                swap(p1y, p2y);
            }
            ans[p1x][p1y] = *p.begin();
            ans[p2x][p2y] = *prev(p.end());
            p.erase(p.begin());
            p.erase(prev(p.end()));
        }

        bool rev = false;
        auto dfs = [&](int sx, int sy) {
            // した右上左の順で埋める
            int x = sx, y = sy;
            while(true) {
                if(ans[x][y]) break;
                if(rev) {
                    ans[x][y] = *prev(p.end());
                    p.erase(prev(p.end()));
                }else {
                    ans[x][y] = *p.begin();
                    p.erase(p.begin());
                }

                if(y == 1 && ans[x][0] == 0) {
                    y = 0;
                    continue;
                }
                if(y == 0 && ans[x][1] == 0) {
                    y = 1;
                    continue;
                }
                rep(i,0,4) {
                    int nx = x + dx[i], ny = y + dy[i];
                    if(nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
                    if(ans[nx][ny] != 0) continue;
                    x = nx, y = ny;
                    break;
                }
            }
        };
        
        if(ans[2][1] == 0) {
            if((2+1) % 2 == 0) rev = true;
            dfs(2, 1);   
        }else if(ans[1][1] == 0){
            vector<int> pp(all(p));
            sort(all(pp));
            ans[1][2] = pp[0];
            ans[0][2] = pp[1];
            ans[0][1] = pp[2];
            ans[1][1] = pp[3];
        }else {
            vector<int> pp(all(p));
            sort(all(pp));
            ans[0][2] = pp[1];
            ans[0][1] = pp[0];
        }
        
    }


    if(sw) {
        vector<vector<int>> res(w, vector<int>(h, 0));
        rep(i,0,h){
            rep(j,0,w) {
                res[j][i] = ans[i][j];
            }
        }
        ans.swap(res);
    }

    for(auto a: ans) {
        for(auto x: a) cout << x << " ";
        cout << endl;
    }
}
0