結果

問題 No.1588 Connection
ユーザー LyricalMaestro
提出日時 2024-02-19 03:13:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 190 ms / 2,000 ms
コード長 1,512 bytes
コンパイル時間 229 ms
コンパイル使用メモリ 82,420 KB
実行使用メモリ 98,492 KB
平均クエリ数 562.34
最終ジャッジ日時 2024-09-29 01:07:01
合計ジャッジ時間 5,310 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/1588

from collections import deque

def main():
    N, M = map(int, input().split())

    cells = [[-1] * N for _ in range(N)]
    passed = [[False] * N for _ in range(N)]

    cells[0][0] = 1
    cells[N - 1][N - 1] = 1
    passed[0][0] = True
    m = 2

    queue = deque()
    queue.append((0, 0))
    while len(queue) > 0:
        i, j = queue.popleft()

        for d_i, d_j in ((-1, 0), (1, 0), (0, 1), (0, -1)):
            new_i = i + d_i
            new_j = j + d_j
            if 0 <= new_i and new_i < N and 0 <= new_j and new_j < N:
                if passed[new_i][new_j]:
                    continue
                if cells[new_i][new_j] == 0:
                    continue

                if cells[new_i][new_j] == 1:
                    passed[new_i][new_j] = True
                    queue.append((new_i, new_j))
                    continue
                
                if M - m > 0:
                    print(f"{new_i + 1} {new_j + 1}")
                    T = input()
                    if T == "White":
                        cells[new_i][new_j] = 0
                    else:
                        m += 1
                        cells[new_i][new_j] = 1
                        passed[new_i][new_j] = True
                        queue.append((new_i, new_j))
                else:
                    cells[new_i][new_j] = 0

    if passed[N - 1][N - 1]:
        print("Yes")
    else:
        print('No')


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