結果

問題 No.359 門松行列
ユーザー maspymaspy
提出日時 2020-03-28 17:21:23
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 1,215 bytes
コンパイル時間 178 ms
コンパイル使用メモリ 10,856 KB
実行使用メモリ 8,228 KB
最終ジャッジ日時 2023-08-30 18:01:53
合計ジャッジ時間 1,433 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,080 KB
testcase_01 AC 16 ms
8,092 KB
testcase_02 AC 17 ms
8,088 KB
testcase_03 AC 22 ms
8,092 KB
testcase_04 AC 18 ms
8,132 KB
testcase_05 AC 19 ms
8,080 KB
testcase_06 AC 16 ms
8,132 KB
testcase_07 AC 19 ms
8,164 KB
testcase_08 AC 28 ms
8,144 KB
testcase_09 AC 28 ms
8,088 KB
testcase_10 AC 29 ms
8,128 KB
testcase_11 AC 26 ms
8,168 KB
testcase_12 AC 25 ms
8,120 KB
testcase_13 AC 25 ms
8,044 KB
testcase_14 AC 22 ms
8,044 KB
testcase_15 AC 19 ms
8,228 KB
testcase_16 AC 22 ms
8,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


T = int(readline())


def test(A):
    if any(x <= 0 for x in A):
        return False

    def is_kadomatsu(a, b, c):
        if a == c:
            return False
        return (a < b > c) or (a > b < c)
    return all(is_kadomatsu(A[i], A[j], A[k]) for i, j, k in ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6)))


def solve():
    L = int(readline())
    A = [0] * 9
    for i in range(3):
        A[3 * i: 3 * i + 3] = map(int, readline().split())
    i, j = (i for i, x in enumerate(A) if not x)
    nums = [L, L + 1]
    for x in A:
        nums.append(x - 1)
        nums.append(x)
        nums.append(x + 1)
        nums.append(L - x + 1)
        nums.append(L - x)
        nums.append(L - x - 1)
    for k in range(3):
        nums.append(L // 2 - k)
        nums.append(L // 2 + k)
    nums = sorted(set(x for x in nums if 1 <= x <= L))
    ret = 0
    for n, m in zip(nums, nums[1:]):
        A[i] = n
        A[j] = L - n
        if test(A):
            ret += m - n
    return ret


for _ in range(T):
    print(solve())
0