結果

問題 No.1384 Bishop and Rook
ユーザー carrot46carrot46
提出日時 2021-02-07 22:34:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,356 ms / 2,000 ms
コード長 3,418 bytes
コンパイル時間 2,257 ms
コンパイル使用メモリ 172,988 KB
実行使用メモリ 18,552 KB
最終ジャッジ日時 2023-09-17 22:46:02
合計ジャッジ時間 19,676 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 769 ms
11,216 KB
testcase_02 AC 360 ms
7,012 KB
testcase_03 AC 139 ms
4,384 KB
testcase_04 AC 198 ms
5,024 KB
testcase_05 AC 531 ms
6,976 KB
testcase_06 AC 718 ms
11,408 KB
testcase_07 AC 857 ms
12,020 KB
testcase_08 AC 648 ms
7,296 KB
testcase_09 AC 15 ms
4,376 KB
testcase_10 AC 445 ms
7,280 KB
testcase_11 AC 11 ms
4,376 KB
testcase_12 AC 20 ms
4,380 KB
testcase_13 AC 12 ms
4,380 KB
testcase_14 AC 1,105 ms
15,704 KB
testcase_15 AC 13 ms
4,380 KB
testcase_16 AC 1,315 ms
18,352 KB
testcase_17 AC 12 ms
4,380 KB
testcase_18 AC 23 ms
4,380 KB
testcase_19 AC 1,069 ms
15,452 KB
testcase_20 AC 11 ms
4,376 KB
testcase_21 AC 35 ms
4,380 KB
testcase_22 AC 34 ms
4,380 KB
testcase_23 AC 29 ms
4,376 KB
testcase_24 AC 35 ms
4,380 KB
testcase_25 AC 35 ms
4,376 KB
testcase_26 AC 33 ms
4,376 KB
testcase_27 AC 32 ms
4,376 KB
testcase_28 AC 32 ms
4,376 KB
testcase_29 AC 33 ms
4,376 KB
testcase_30 AC 29 ms
4,376 KB
testcase_31 AC 22 ms
4,376 KB
testcase_32 AC 28 ms
4,376 KB
testcase_33 AC 27 ms
4,376 KB
testcase_34 AC 25 ms
4,380 KB
testcase_35 AC 26 ms
4,376 KB
testcase_36 AC 19 ms
4,380 KB
testcase_37 AC 18 ms
4,376 KB
testcase_38 AC 18 ms
4,380 KB
testcase_39 AC 17 ms
4,376 KB
testcase_40 AC 21 ms
4,376 KB
testcase_41 AC 135 ms
4,380 KB
testcase_42 AC 126 ms
4,380 KB
testcase_43 AC 151 ms
4,376 KB
testcase_44 AC 174 ms
4,376 KB
testcase_45 AC 168 ms
4,376 KB
testcase_46 AC 138 ms
4,380 KB
testcase_47 AC 154 ms
4,376 KB
testcase_48 AC 147 ms
4,380 KB
testcase_49 AC 86 ms
4,376 KB
testcase_50 AC 86 ms
4,376 KB
testcase_51 AC 4 ms
4,376 KB
testcase_52 AC 1,356 ms
18,552 KB
testcase_53 AC 561 ms
7,208 KB
testcase_54 AC 5 ms
4,376 KB
testcase_55 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T b){
    if(a < b) {a = b; return true;}
    else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
    if(!v.empty()){
        for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
        std::cout<<v.back();
    }
    if(endline) std::cout<<std::endl;
}

void solve(){
    if(N == 1 && M == 1){
        cout<<"0\n1 1"<<endl;
    }else if(N == 1 || M == 1){
        cout<<"-1"<<endl;
    }else if(N == 2 || M == 2){
        bool sp = M == 2;
        if(sp) swap(N, M);
        if(M&1){
            cout<<-1<<endl;
        }else {
            vec x(N * M), y(N * M);
            rep(j, N * M / 4){
                int l = j * 2, i = j * 4;
                x[i] = l + 1; y[i] = 1;
                x[i+1] = l + 2; y[i+1] = 2;
                x[i+2] = l + 1; y[i+2] = 2;
                x[i+3] = l + 2; y[i+3] = 1;
            }
            if(sp) swap(x, y);
            cout<<N * M - 1<<endl;
            rep(i, N * M) cout<<y[i]<<' '<<x[i]<<endl;
        }
    }else if(N == 4 || M == 4){
        bool sp = M == 4;
        if(sp) swap(N, M);
        if(M&1){
            cout<<-1<<endl;
        }else {
            vec x(N * M), y(N * M);
            rep(j, M / 2){
                int l = j * 2, i = j * 4;
                x[i] = l + 1; y[i] = 3;
                x[i+1] = l + 2; y[i+1] = 4;
                x[i+2] = l + 1; y[i+2] = 4;
                x[i+3] = l + 2; y[i+3] = 3;
            }
            reps(i, M * 2, M * 4) {
                x[i] = M + 1 - x[i - M * 2];
                y[i] = 5 - y[i - M * 2];
            }
            if(sp) swap(x, y);
            cout<<N * M - 1<<endl;
            rep(i, N * M) cout<<y[i]<<' '<<x[i]<<endl;
        }
    }else if(N%2 == 0 && M%2 == 0){
        cout<<N * M - 1<<endl;
        vec x(N * M), y(N * M);
        x[0] = 2; y[0] = 1;
        x[1] = 1; y[1] = 2;
        x[2] = 1; y[2] = 1;
        x[3] = 2; y[3] = 2;
        reps(h, 1, N / 2){
            int u = h * 2, i = h * 4;
            x[i] = u + 1; y[i] = 2;
            x[i+1] = u + 2; y[i+1] = 1;
            x[i+2] = u + 1; y[i+2] = 1;
            x[i+3] = u + 2; y[i+3] = 2;
        }
        reps(l, 1, M / 2){
            int i = l * N * 2;
            if(l&1){
                reps(j, i, i + N * 2) {
                    x[j] = x[i + N * 2 - 1 - j];
                    y[j] = (l + 1) * 2 + 1 - y[i + N * 2 - 1 - j];
                }
            }else{
                reps(j, i, i + N * 2) {
                    x[j] = x[j - i];
                    y[j] = y[j - i] + l * 2;
                }
            }
        }
        rep(i, N * M) cout<<x[i]<<' '<<y[i]<<endl;
    }else{
        cout<<-1<<endl;
    }


}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin>>Q;
    rep(_, Q) {
        cin>>N>>M;
        solve();
    }
}
0