結果

問題 No.323 yuki国
ユーザー rpy3cpprpy3cpp
提出日時 2015-12-17 23:54:31
言語 Python2
(2.7.18)
結果
AC  
実行時間 36 ms / 5,000 ms
コード長 4,361 bytes
コンパイル時間 373 ms
コンパイル使用メモリ 6,800 KB
実行使用メモリ 6,124 KB
最終ジャッジ日時 2023-09-10 21:21:36
合計ジャッジ時間 2,327 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
6,016 KB
testcase_01 AC 11 ms
5,952 KB
testcase_02 AC 11 ms
5,988 KB
testcase_03 AC 11 ms
5,852 KB
testcase_04 AC 12 ms
5,892 KB
testcase_05 AC 11 ms
5,876 KB
testcase_06 AC 11 ms
5,984 KB
testcase_07 AC 12 ms
6,024 KB
testcase_08 AC 11 ms
6,012 KB
testcase_09 AC 19 ms
5,980 KB
testcase_10 AC 11 ms
5,988 KB
testcase_11 AC 11 ms
5,888 KB
testcase_12 AC 11 ms
6,020 KB
testcase_13 AC 19 ms
5,948 KB
testcase_14 AC 12 ms
5,892 KB
testcase_15 AC 36 ms
5,996 KB
testcase_16 AC 27 ms
6,020 KB
testcase_17 AC 20 ms
6,068 KB
testcase_18 AC 14 ms
5,936 KB
testcase_19 AC 11 ms
5,932 KB
testcase_20 AC 12 ms
5,984 KB
testcase_21 AC 12 ms
5,856 KB
testcase_22 AC 19 ms
6,124 KB
testcase_23 AC 12 ms
5,984 KB
testcase_24 AC 23 ms
6,028 KB
testcase_25 AC 12 ms
5,852 KB
testcase_26 AC 27 ms
6,048 KB
testcase_27 AC 11 ms
5,988 KB
testcase_28 AC 27 ms
6,000 KB
testcase_29 AC 28 ms
6,096 KB
testcase_30 AC 11 ms
5,876 KB
testcase_31 AC 11 ms
6,020 KB
testcase_32 AC 11 ms
5,848 KB
testcase_33 AC 11 ms
5,924 KB
testcase_34 AC 12 ms
5,996 KB
testcase_35 AC 11 ms
5,996 KB
testcase_36 AC 11 ms
5,988 KB
testcase_37 AC 11 ms
6,016 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