結果

問題 No.359 門松行列
ユーザー mamekinmamekin
提出日時 2016-04-17 23:10:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,251 bytes
コンパイル時間 1,199 ms
コンパイル使用メモリ 113,772 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-01 07:45:38
合計ジャッジ時間 2,235 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

bool checkKadomatsu(const vector<int>& a)
{
    if(a[0] == a[2])
        return false;
    return (a[0] < a[1] && a[2] < a[1]) || (a[0] > a[1] && a[2] > a[1]);
}

bool checkKadomatsu(const vector<vector<int> >& a)
{
    for(int i=0; i<3; ++i){
        vector<int> b(3), c(3);
        for(int j=0; j<3; ++j){
            b[j] = a[i][j];
            c[j] = a[j][i];
        }
        if(!checkKadomatsu(b) || !checkKadomatsu(c))
            return false;
    }

    vector<int> b(3), c(3);
    for(int i=0; i<3; ++i){
        b[i] = a[i][i];
        c[i] = a[i][2-i];
    }
    return checkKadomatsu(b) && checkKadomatsu(c);
}

int main()
{
    int t;
    cin >> t;

    while(--t >= 0){
        int len;
        cin >> len;

        vector<vector<int> > a(3, vector<int>(3));
        vector<int> y, x;
        set<int> s;
        s.insert(1);
        s.insert(len);
        s.insert(len/2-1);
        s.insert(len/2);
        s.insert(len/2+1);
        for(int i=0; i<3; ++i){
            for(int j=0; j<3; ++j){
                cin >> a[i][j];
                if(a[i][j] == 0){
                    y.push_back(i);
                    x.push_back(j);
                }
                else{
                    for(int k=-1; k<=1; ++k){
                        s.insert(a[i][j] + k);
                        s.insert(len - a[i][j] + k);
                    }
                }
            }
        }

        vector<int> v(s.begin(), s.end());
        int ans = 0;
        for(unsigned i=0; i<v.size()-1; ++i){
            int p = v[i];
            int q = v[i+1];
            if(!(0 < p && p < len))
                continue;

            a[y[0]][x[0]] = p;
            a[y[1]][x[1]] = len - p;
            if(checkKadomatsu(a))
                ans += q - p;
        }
        cout << ans << endl;
    }

    return 0;
}
0