結果

問題 No.179 塗り分け
ユーザー hang_hang_clnhang_hang_cln
提出日時 2018-06-09 11:39:31
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,372 ms / 3,000 ms
コード長 2,336 bytes
コンパイル時間 79 ms
コンパイル使用メモリ 10,868 KB
実行使用メモリ 8,612 KB
最終ジャッジ日時 2023-09-30 21:02:05
合計ジャッジ時間 14,674 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,456 KB
testcase_01 AC 18 ms
8,472 KB
testcase_02 AC 19 ms
8,520 KB
testcase_03 AC 19 ms
8,408 KB
testcase_04 AC 20 ms
8,324 KB
testcase_05 AC 20 ms
8,556 KB
testcase_06 AC 42 ms
8,428 KB
testcase_07 AC 19 ms
8,400 KB
testcase_08 AC 802 ms
8,564 KB
testcase_09 AC 36 ms
8,500 KB
testcase_10 AC 419 ms
8,564 KB
testcase_11 AC 356 ms
8,564 KB
testcase_12 AC 718 ms
8,612 KB
testcase_13 AC 21 ms
8,568 KB
testcase_14 AC 24 ms
8,332 KB
testcase_15 AC 19 ms
8,568 KB
testcase_16 AC 19 ms
8,400 KB
testcase_17 AC 19 ms
8,396 KB
testcase_18 AC 441 ms
8,516 KB
testcase_19 AC 380 ms
8,468 KB
testcase_20 AC 20 ms
8,432 KB
testcase_21 AC 20 ms
8,532 KB
testcase_22 AC 1,372 ms
8,456 KB
testcase_23 AC 421 ms
8,596 KB
testcase_24 AC 847 ms
8,508 KB
testcase_25 AC 615 ms
8,508 KB
testcase_26 AC 266 ms
8,504 KB
testcase_27 AC 819 ms
8,456 KB
testcase_28 AC 244 ms
8,540 KB
testcase_29 AC 803 ms
8,412 KB
testcase_30 AC 336 ms
8,508 KB
testcase_31 AC 805 ms
8,596 KB
testcase_32 AC 573 ms
8,600 KB
testcase_33 AC 802 ms
8,456 KB
testcase_34 AC 414 ms
8,448 KB
testcase_35 AC 809 ms
8,560 KB
testcase_36 AC 19 ms
8,532 KB
testcase_37 AC 19 ms
8,404 KB
testcase_38 AC 19 ms
8,404 KB
testcase_39 AC 19 ms
8,424 KB
testcase_40 AC 19 ms
8,520 KB
testcase_41 AC 20 ms
8,412 KB
testcase_42 AC 19 ms
8,516 KB
testcase_43 AC 42 ms
8,532 KB
testcase_44 AC 120 ms
8,432 KB
testcase_45 AC 23 ms
8,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from copy import deepcopy

H, W = map(int,input().split())
    
field = []
for h in range(H):
    field.append(list(input()))

def painting(dx, dy,black_cnt):
    remainCnt = deepcopy(black_cnt)
    # ある平行移動量で塗れるか判定
    paintCnt = 0 # 赤で塗った枚数
    canPaintByThiWay = True # この平行移動量で塗り分け可能か
    paint = [[0 for w in range(W)] for h in range(H)] # 0:塗ってない 1:赤 2:青
    for h in range(H):
        for w in range(W):
            if field[h][w] == '#' and paint[h][w] == 0: # 赤で塗ることができる
                paint[h][w] = 1 # 赤で塗る
                paintCnt += 1
                remainCnt -= 1
                # 平行移動先に青でも塗ってみる
                if h+dy <0 or w+dx<0 or h+dy >= H or w+dx >= W: # フィールドの外に移動しようとしていたら
                    return False
                elif field[h+dy][w+dx] == '#' and paint[h+dy][w+dx] == 0: # 青で塗ることができる
                    paint[h+dy][w+dx] = 2 # 青で塗る
                    remainCnt -= 1
                else: # 青で塗ることが出来ない = 失敗
                    return False # この平行移動量ではむり
            #else: # 赤で塗ることができない = 失敗とは限らない
            #    if paint[h][w] == 2: # 青ですでに塗られていたら
            #        return False # この平行移動量ではむり

            if remainCnt == 0:
                if paintCnt * 2 == black_cnt:
                    return True
                else:
                    return False

def check():
    black_cnt = 0
    for h in range(H):
        for w in range(W):
            #print(field[h][w])
            if field[h][w] == '#':
                black_cnt += 1
    if black_cnt % 2 == 1:
        return False
    elif black_cnt == 0:
        return False
    else:
        # 平行移動量を決める
        for dx in range(-W,W):
            for dy in range(-H,H):
                if dx == 0 and dy == 0:
                    continue
                
                if painting(dx,dy,black_cnt):
                    return True
                else:
                    canPaint = False
        
        return canPaint

if check():
    print('YES')
else:
    print('NO')
0