結果

問題 No.323 yuki国
ユーザー rpy3cpprpy3cpp
提出日時 2015-12-18 10:00:27
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 160 ms / 5,000 ms
コード長 4,361 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 77,700 KB
実行使用メモリ 81,056 KB
最終ジャッジ日時 2023-09-10 21:22:01
合計ジャッジ時間 5,246 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
76,476 KB
testcase_01 AC 74 ms
76,220 KB
testcase_02 AC 74 ms
76,340 KB
testcase_03 AC 77 ms
76,424 KB
testcase_04 AC 75 ms
76,428 KB
testcase_05 AC 77 ms
76,496 KB
testcase_06 AC 75 ms
76,360 KB
testcase_07 AC 76 ms
76,344 KB
testcase_08 AC 77 ms
77,012 KB
testcase_09 AC 139 ms
79,708 KB
testcase_10 AC 75 ms
76,556 KB
testcase_11 AC 75 ms
76,256 KB
testcase_12 AC 73 ms
76,312 KB
testcase_13 AC 102 ms
79,272 KB
testcase_14 AC 80 ms
78,220 KB
testcase_15 AC 151 ms
80,144 KB
testcase_16 AC 99 ms
79,412 KB
testcase_17 AC 123 ms
80,392 KB
testcase_18 AC 100 ms
79,456 KB
testcase_19 AC 75 ms
77,116 KB
testcase_20 AC 86 ms
79,104 KB
testcase_21 AC 80 ms
78,388 KB
testcase_22 AC 131 ms
80,204 KB
testcase_23 AC 80 ms
78,348 KB
testcase_24 AC 130 ms
80,504 KB
testcase_25 AC 80 ms
78,556 KB
testcase_26 AC 130 ms
80,388 KB
testcase_27 AC 81 ms
77,972 KB
testcase_28 AC 160 ms
81,044 KB
testcase_29 AC 160 ms
81,056 KB
testcase_30 AC 74 ms
76,716 KB
testcase_31 AC 75 ms
76,812 KB
testcase_32 AC 75 ms
76,280 KB
testcase_33 AC 74 ms
76,272 KB
testcase_34 AC 75 ms
76,544 KB
testcase_35 AC 75 ms
76,788 KB
testcase_36 AC 74 ms
76,468 KB
testcase_37 AC 74 ms
76,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

input = raw_input

def read_data():
    H, W = map(int, input().split())
    A, Si, Sj = map(int, input().split())
    B, Gi, Gj = map(int, input().split())
    Ms = [[c == '*' for c in input()] for i in range(H)]
    return H, W, A, Si, Sj, B, Gi, Gj, Ms


def solve(H, W, A, Si, Sj, B, Gi, Gj, Ms):
    if H == 1 and W == 1:
        return False
    if (A - B + (Si - Gi) + (Sj - Gj)) % 2:
        return False
    lower = [[float('inf')] * W for h in range(H)]  # lower[i][j]: (i,j)での雪玉の大きさの最小値。初期値は inf でまだ雪玉が到達していないことを示す。
    upper = [[-1] * W for h in range(H)]  # upper[i][j]: (i,j)での雪玉の大きさの最大値。初期値は -1 でまだ到達していないことを示す。
    lower[Si][Sj] = A  # 始点での雪玉の大きさは、最初は下限も上限も A に等しい。
    upper[Si][Sj] = A
    fs = set([(Si, Sj)])  # 幅優先探索での最前線の位置を示す集合
    while fs:
        nfs = set()       # 幅優先探索での次のステップでの最前線を示す集合
        for i, j in fs:
            m = Ms[i][j]
            min_snow = lower[i][j]
            max_snow = upper[i][j]
            for ni, nj in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]:
                if ni < 0 or ni >= H or nj < 0 or nj >= W:
                    continue
                if update_status(m, min_snow, max_snow, ni, nj, upper, lower, Ms):
                    nfs.add((ni, nj))  # 変更があったら、(ni, nj) を次の前線に入れる。
        if lower[Gi][Gj] <= B and upper[Gi][Gj] >= B:  # 終点での雪玉の目標値 B が上限加減の範囲に入ったら成功。
            return True
        fs = nfs
    return False

def update_status(m, min_snow, max_snow, ni, nj, upper, lower, Ms):
    n_m = Ms[ni][nj]
    if m != n_m:
        if n_m:  # 移動元には雪がなく、移動先には雪がある場合。
            n_min_snow = min_snow + 1
            n_max_snow = max_snow + 1
        else:   # 移動元には雪があり、移動先には雪がない場合。
            n_min_snow = min_snow - 1
            n_max_snow = max_snow - 1
            if min_snow == 1:
                if max_snow == 1:  # 移動元の雪玉の上限下限がともに 1 の場合は、移動先に雪がないので進めない。
                    n_min_snow = float('inf')
                    n_max_snow = -1
                else:              # 移動元の雪玉の下限が 1 で、上限が 1より大きい(2はありえないので3以上)ときは、移動先での下限が2になる。
                    n_min_snow = 2
    elif n_m:  # 移動元と移動先の両方に雪がある場合。ここを何度も往復すれば、雪玉をいくらでも大きくできる。
        n_min_snow = min_snow + 1
        n_max_snow = float('inf')
    else:     #  移動元と移動先の両方に雪がない場合。ここを何度も往復すれば、雪玉を 2 or 1 になるまで小さくできる。
        if min_snow == 1:
            if max_snow == 1:  # 移動元の雪玉の上限下限がともに 1 の場合は、移動先に雪がないので進めない。
                n_min_snow = float('inf')
            else:              # 移動元の雪玉の下限が 1 で、上限が 1より大きい(2はありえないので3以上)ときは、移動先での下限が2になる。
                n_min_snow = 2
        elif min_snow % 2:   # 移動元の雪玉の下限が 1 より大きい奇数(3以上の奇数)の場合は、移動先の下限は 2 になる。
            n_min_snow = 2
        else:                # 移動元の雪玉の下限が 偶数の場合は、移動先の下限は 1 になる。
            n_min_snow = 1
        n_max_snow = max_snow - 1
        if max_snow == 1:  # 移動元の雪玉の上限が 1 のときは、移動先に雪がないので進めない。
            n_max_snow = -1
    changed = False        
    if n_min_snow < lower[ni][nj]:
        lower[ni][nj] = n_min_snow
        changed = True
    if n_max_snow > upper[ni][nj]:
        upper[ni][nj] = n_max_snow
        changed = True
    return changed
                

H, W, A, Si, Sj, B, Gi, Gj, Ms = read_data()
if solve(H, W, A, Si, Sj, B, Gi, Gj, Ms):
    print('Yes')
else:
    print('No')
0