結果

問題 No.3803 Crossword Key Checker
コンテスト
ユーザー Skyシャーレ
提出日時 2026-10-07 13:42:22
言語 Python3
(3.15.2 + numpy 2.5.4 + scipy 1.18.1 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 28 ms / 2,000 ms
+ 464µs
コード長 1,623 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 52 ms
コンパイル使用メモリ 14,976 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2026-11-14 14:32:20
合計ジャッジ時間 3,224 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 47
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys

def main():
    t = sys.stdin.read().split()
    H, W = int(t[0]), int(t[1])
    grid = t[2:2 + H]
    K = int(t[2 + H])
    base = 3 + H
    R = [t[base + 2 * k] for k in range(K)]
    C = [t[base + 2 * k + 1] for k in range(K)]

    def white(i, j):
        return 0 <= i < H and 0 <= j < W and grid[i][j] == '.'

    letters = {}  # マス -> 書き込まれた文字

    def place(i, j, di, dj, s, exists):
        # カギがないなら '-'、あるなら '-' 以外が必要
        if not exists:
            return s == '-'
        if s == '-':
            return False
        # 連続する白マスの数と文字列の長さを比較
        n = 0
        while white(i + di * n, j + dj * n):
            n += 1
        if n != len(s):
            return False
        # 重なるマスの文字が一致するか確認
        for k, ch in enumerate(s):
            cell = (i + di * k, j + dj * k)
            if letters.setdefault(cell, ch) != ch:
                return False
        return True

    idx = 0
    for i in range(H):
        for j in range(W):
            if not white(i, j):
                continue
            a = not white(i, j - 1) and white(i, j + 1)  # 横のカギ
            d = not white(i - 1, j) and white(i + 1, j)  # 縦のカギ
            if not (a or d):
                continue
            if idx >= K:
                print("No")
                return
            if not place(i, j, 0, 1, R[idx], a) or not place(i, j, 1, 0, C[idx], d):
                print("No")
                return
            idx += 1

    print("Yes" if idx == K else "No")

main()
0