結果

問題 No.86 TVザッピング(2)
ユーザー titiatitia
提出日時 2022-01-13 03:14:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 998 ms / 5,000 ms
コード長 2,186 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 87,196 KB
実行使用メモリ 91,788 KB
最終ジャッジ日時 2023-08-26 15:04:22
合計ジャッジ時間 6,890 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,492 KB
testcase_01 AC 71 ms
71,228 KB
testcase_02 AC 72 ms
71,316 KB
testcase_03 AC 71 ms
71,356 KB
testcase_04 AC 70 ms
71,316 KB
testcase_05 AC 70 ms
71,312 KB
testcase_06 AC 70 ms
71,444 KB
testcase_07 AC 74 ms
71,416 KB
testcase_08 AC 76 ms
75,568 KB
testcase_09 AC 552 ms
86,516 KB
testcase_10 AC 998 ms
91,664 KB
testcase_11 AC 83 ms
76,008 KB
testcase_12 AC 70 ms
71,336 KB
testcase_13 AC 73 ms
71,424 KB
testcase_14 AC 107 ms
77,480 KB
testcase_15 AC 138 ms
77,820 KB
testcase_16 AC 141 ms
78,032 KB
testcase_17 AC 73 ms
71,452 KB
testcase_18 AC 92 ms
76,080 KB
testcase_19 AC 82 ms
76,416 KB
testcase_20 AC 78 ms
75,852 KB
testcase_21 AC 936 ms
91,788 KB
testcase_22 AC 72 ms
71,260 KB
testcase_23 AC 115 ms
77,204 KB
testcase_24 AC 78 ms
75,724 KB
testcase_25 AC 71 ms
71,536 KB
testcase_26 AC 72 ms
71,228 KB
testcase_27 AC 71 ms
71,336 KB
testcase_28 AC 71 ms
71,624 KB
testcase_29 AC 383 ms
84,156 KB
testcase_30 AC 71 ms
71,488 KB
testcase_31 AC 72 ms
71,380 KB
testcase_32 AC 71 ms
71,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M=map(int,input().split())
MAP=[input() for i in range(N)]

def findmap(x0,y0,di):
    USE=[[0]*M for i in range(N)]

    Q=[(x0,y0,di)]
    while Q:
        x,y,di=Q.pop()
        if USE[x][y]==1:
            if x==x0 and y==y0:
                break
            else:
                return False
            
        USE[x][y]=1

        if di=="R":
            if 0<=x<N and 0<=y+1<M and MAP[x][y+1]==".":
                Q.append((x,y+1,"R"))
            elif 0<=x-1<N and 0<=y<M and MAP[x-1][y]==".":
                Q.append((x-1,y,"U"))
            else:
                return False

        elif di=="L":
            if 0<=x<N and 0<=y-1<M and MAP[x][y-1]==".":
                Q.append((x,y-1,"L"))
            elif 0<=x+1<N and 0<=y<M and MAP[x+1][y]==".":
                Q.append((x+1,y,"D"))
            else:
                return False

        elif di=="U":
            if 0<=x-1<N and 0<=y<M and MAP[x-1][y]==".":
                Q.append((x-1,y,"U"))
            elif 0<=x<N and 0<=y-1<M and MAP[x][y-1]==".":
                Q.append((x,y-1,"L"))
            else:
                return False

        elif di=="D":
            if 0<=x+1<N and 0<=y<M and MAP[x+1][y]==".":
                Q.append((x+1,y,"D"))
            elif 0<=x<N and 0<=y+1<M and MAP[x][y+1]==".":
                Q.append((x,y+1,"R"))
            else:
                return False

    for i in range(N):
        for j in range(M):
            if MAP[i][j]=="." and USE[i][j]==0:
                return False
    return True

for x in range(N):
    for y in range(M):
        if 0<=x+1<N and 0<=y<M and MAP[x+1][y]==".":
            if findmap(x,y,"D")==True:
                print("YES")
                exit()
        if 0<=x-1<N and 0<=y<M and MAP[x-1][y]==".":
            if findmap(x,y,"U")==True:
                print("YES")
                exit()
        if 0<=x<N and 0<=y+1<M and MAP[x][y+1]==".":
            if findmap(x,y,"R")==True:
                print("YES")
                exit()
        if 0<=x<N and 0<=y-1<M and MAP[x][y-1]==".":
            if findmap(x,y,"L")==True:
                print("YES")
                exit()
else:
    print("NO")












        
0