結果

問題 No.2708 Jewel holder
コンテスト
ユーザー KDKJ
提出日時 2024-04-29 19:42:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 184 ms / 2,000 ms
コード長 1,571 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 416 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 89,344 KB
最終ジャッジ日時 2024-11-19 02:33:12
合計ジャッジ時間 4,892 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from heapq import heappush, heappop, heapify
import sys
from collections import defaultdict, deque,Counter
from math import ceil, floor, sqrt, factorial,gcd
from itertools import permutations, combinations,product
from bisect import bisect_left, bisect_right
from copy import deepcopy
from functools import lru_cache #@lru_cache(maxsize=None)
from fractions import Fraction
sys.setrecursionlimit(10**6)
# input = sys.stdin.readline
vector1 = [[0, -1], [1, 0], [0, 1], [-1, 0]]
vector2 = [[0, 1], [1, 0], [-1, 0], [0, -1],
           [1,-1], [-1, 1], [1, 1], [-1, -1]]




def main():
    h,w = map(int,input().split())
    a = [input() for i in range(h)]
    q = deque()
    q.append([0,0,1])
    memo = defaultdict(int)
    md = defaultdict(lambda:-1)
    memo[(0,0)] = 1
    md[(0,0)] = 0
    ans = 0

    while q:
        x,y,c = q.popleft()
        if x==h-1 and y==w-1:
            ans += 1
        for dx,dy in vector1:
            nx = x+dx
            ny = y+dy
            if 0<=nx<h and 0<=ny<w and a[nx][ny] != "#"\
                and (md[(nx,ny)] ==-1 or md[(nx,ny)] ==  md[(x,y)]+1):
                if c == 0:
                    if a[nx][ny] == "o":
                        md[(nx,ny)] = md[(x,y)] + 1
                        q.append([nx,ny,c+1])
                else:
                    md[(nx,ny)] = md[(x,y)] + 1
                    if a[nx][ny] == "o":
                        q.append([nx,ny,c+1])
                    else:
                        q.append([nx,ny,c-1])
    print(ans)                    
    
if __name__ == '__main__':
    main()
0