結果

問題 No.2402 Dirty Stairs and Shoes
ユーザー lam6er
提出日時 2025-03-20 18:59:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 1,896 bytes
コンパイル時間 315 ms
コンパイル使用メモリ 82,320 KB
実行使用メモリ 111,192 KB
最終ジャッジ日時 2025-03-20 19:00:33
合計ジャッジ時間 4,331 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    n, K = map(int, sys.stdin.readline().split())
    m1 = int(sys.stdin.readline())
    a_list = list(map(int, sys.stdin.readline().split())) if m1 > 0 else []
    dirty = set(a_list)
    m2 = int(sys.stdin.readline())
    b_list = list(map(int, sys.stdin.readline().split())) if m2 > 0 else []
    mat = set(b_list)

    # Visited arrays for clean and dirty states
    visited_clean = [False] * (n + 1)
    visited_dirty = [False] * (n + 1)

    queue = deque()
    queue.append((0, True))
    visited_clean[0] = True

    found = False

    while queue:
        p, is_clean = queue.popleft()
        for move in [1, K]:
            next_p = p + move
            if next_p > n:
                continue
            # Check if next_p is the target N
            if next_p == n:
                # The new state is same as current since N is neither dirty nor mat
                if is_clean:
                    found = True
                    break
                else:
                    continue
            else:
                # Determine the new state based on next_p's type
                if next_p in dirty:
                    new_clean = False
                elif next_p in mat:
                    new_clean = True
                else:
                    new_clean = is_clean
                # Check if this state has been visited before
                if new_clean:
                    if not visited_clean[next_p]:
                        visited_clean[next_p] = True
                        queue.append((next_p, new_clean))
                else:
                    if not visited_dirty[next_p]:
                        visited_dirty[next_p] = True
                        queue.append((next_p, new_clean))
        if found:
            break

    print("Yes" if found else "No")

if __name__ == "__main__":
    main()
0