結果

問題 No.86 TVザッピング(2)
ユーザー tnodinotnodino
提出日時 2022-03-05 15:22:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 2,914 bytes
コンパイル時間 750 ms
コンパイル使用メモリ 11,128 KB
実行使用メモリ 11,164 KB
最終ジャッジ日時 2023-09-27 02:39:28
合計ジャッジ時間 2,515 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,620 KB
testcase_01 AC 19 ms
8,720 KB
testcase_02 AC 19 ms
8,840 KB
testcase_03 AC 20 ms
8,628 KB
testcase_04 AC 22 ms
8,824 KB
testcase_05 AC 19 ms
8,720 KB
testcase_06 AC 19 ms
8,820 KB
testcase_07 AC 20 ms
8,760 KB
testcase_08 AC 30 ms
8,932 KB
testcase_09 AC 24 ms
8,828 KB
testcase_10 AC 54 ms
11,164 KB
testcase_11 AC 22 ms
8,716 KB
testcase_12 AC 20 ms
8,824 KB
testcase_13 AC 20 ms
8,620 KB
testcase_14 AC 30 ms
9,028 KB
testcase_15 AC 25 ms
8,844 KB
testcase_16 AC 24 ms
8,880 KB
testcase_17 AC 20 ms
8,692 KB
testcase_18 AC 20 ms
8,732 KB
testcase_19 AC 20 ms
8,696 KB
testcase_20 AC 20 ms
8,800 KB
testcase_21 AC 41 ms
10,012 KB
testcase_22 AC 19 ms
8,760 KB
testcase_23 AC 30 ms
9,036 KB
testcase_24 WA -
testcase_25 AC 20 ms
8,728 KB
testcase_26 AC 20 ms
8,712 KB
testcase_27 AC 20 ms
8,712 KB
testcase_28 AC 21 ms
8,792 KB
testcase_29 AC 24 ms
8,956 KB
testcase_30 AC 20 ms
8,760 KB
testcase_31 WA -
testcase_32 AC 21 ms
8,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit
setrecursionlimit(10**6)

from copy import deepcopy
def Check1(N, M, A):
    def dfs(x, y, rx, ry):
        for i in range(4):
            nx = x + dx[i]
            my = y + dy[i]
            if nx < 0 or N <= nx or my < 0 or M <= my:
                continue
            if nx == rx and my == ry:
                continue
            if L[nx][my] == '#':
                continue
            L[nx][my] = '#'
            dfs(nx, my, x, y)
    L = deepcopy(A)
    flg = False
    for i in range(N):
        for j in range(M):
            if L[i][j] == '.':
                if flg:
                    print('NO')
                    exit()
                dfs(i, j, -1, -1)
                flg = True

def Check2(N, M, A):
    def Count(x, y):
        ret = 0
        for i in range(4):
            nx = x + dx[i]
            my = y + dy[i]
            if nx < 0 or N <= nx or my < 0 or M <= my:
                continue
            if A[nx][my] == '.':
                ret += 1
        return ret
    cnt = [[0] * M for _ in range(N)]
    for i in range(N):
        for j in range(M):
            if A[i][j] == '.':
                cnt[i][j] = Count(i, j)
                if cnt[i][j] == 1:
                    print('NO')
                    exit()
                cnt[i][j] -= 2
    return cnt

def Check3(N, M, cnt):
    def dfs(x, y, rx, ry):
        for i in range(4):
            nx = x + dx[i]
            my = y + dy[i]
            if nx < 0 or N <= nx or my < 0 or M <= my:
                continue
            if nx == rx and my == ry:
                continue
            if L[nx][my] == 0:
                continue
            L[nx][my] = 0
            dfs(nx, my, x, y)
    L = deepcopy(cnt)
    flg = False
    for i in range(N):
        for j in range(M):
            if L[i][j] > 0:
                if flg:
                    print('NO')
                    exit()
                dfs(i, j, -1, -1)
                flg = True

def Check4(N, M, cnt):
    flg = False
    for i in range(N):
        for j in range(M):
            if cnt[i][j] == 2:
                if flg:
                    print('NO')
                    exit()
                flg = True

def Check5(N, M, cnt):
    def dfs(x, y):
        ret = 0
        for i in range(4):
            nx = x + dx[i]
            my = y + dy[i]
            if nx < 0 or N <= nx or my < 0 or M <= my:
                continue
            ret += cnt[nx][my]
        return ret
    for i in range(N):
        for j in range(M):
            if cnt[i][j] == 2:
                if dfs(i, j) == 4:
                    print('NO')
                    exit()

N,M = map(int,input().split())
A = [list(input()) for _ in range(N)]
if N == 1 or M == 1:
    print('NO')
    exit()
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
Check1(N, M, A)
cnt = Check2(N, M, A)
Check3(N, M, cnt)
Check4(N, M, cnt)
Check5(N, M, cnt)
print('YES')
0