結果

問題 No.86 TVザッピング(2)
ユーザー tnodinotnodino
提出日時 2022-03-05 15:21:39
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 2,854 bytes
コンパイル時間 98 ms
コンパイル使用メモリ 11,176 KB
実行使用メモリ 9,376 KB
最終ジャッジ日時 2023-09-27 02:38:40
合計ジャッジ時間 2,470 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,688 KB
testcase_01 AC 20 ms
8,688 KB
testcase_02 AC 19 ms
8,752 KB
testcase_03 AC 20 ms
8,816 KB
testcase_04 AC 19 ms
8,688 KB
testcase_05 AC 20 ms
8,604 KB
testcase_06 AC 19 ms
8,704 KB
testcase_07 AC 19 ms
8,616 KB
testcase_08 AC 29 ms
9,148 KB
testcase_09 AC 24 ms
8,852 KB
testcase_10 RE -
testcase_11 AC 20 ms
8,676 KB
testcase_12 AC 19 ms
8,564 KB
testcase_13 AC 20 ms
8,612 KB
testcase_14 AC 29 ms
9,040 KB
testcase_15 AC 25 ms
8,780 KB
testcase_16 AC 24 ms
8,840 KB
testcase_17 AC 20 ms
8,684 KB
testcase_18 AC 20 ms
8,680 KB
testcase_19 AC 20 ms
8,612 KB
testcase_20 AC 19 ms
8,760 KB
testcase_21 RE -
testcase_22 AC 20 ms
8,692 KB
testcase_23 AC 30 ms
9,080 KB
testcase_24 WA -
testcase_25 AC 19 ms
8,620 KB
testcase_26 AC 19 ms
8,556 KB
testcase_27 AC 19 ms
8,800 KB
testcase_28 AC 20 ms
8,708 KB
testcase_29 AC 23 ms
8,808 KB
testcase_30 AC 20 ms
8,604 KB
testcase_31 WA -
testcase_32 AC 19 ms
8,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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