結果
| 問題 | 
                            No.179 塗り分け
                             | 
                    
| コンテスト | |
| ユーザー | 
                             はむ吉🐹
                         | 
                    
| 提出日時 | 2016-03-04 22:01:56 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 566 ms / 3,000 ms | 
| コード長 | 1,493 bytes | 
| コンパイル時間 | 685 ms | 
| コンパイル使用メモリ | 82,688 KB | 
| 実行使用メモリ | 77,316 KB | 
| 最終ジャッジ日時 | 2024-07-23 14:34:18 | 
| 合計ジャッジ時間 | 5,152 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 6 | 
| other | AC * 40 | 
ソースコード
#!/usr/bin/env pypy3
# -*- coding: utf-8 -*-
import array
import collections
import itertools
IS_RED = 1
IS_BLUE = 2
def solve(height, width, black_cells):
    num_black_cells = len(black_cells)
    if num_black_cells == 0:
        return False
    elif num_black_cells % 2 == 1:
        return False
    it_a = ((0, dc) for dc in range(1, width))
    it_b = itertools.product(range(1, height), range(-width + 1, width))
    for dr, dc in itertools.chain(it_a, it_b):
        counter = 0
        painted = collections.defaultdict(int)
        for r0, c0 in black_cells:
            if counter >= num_black_cells:
                return True
            (r, c) = (r0 + dr, c0 + dc)
            if r < 0 or r >= height or c < 0 or c >= width:
                if not painted[(r0, c0)]:
                    break
            elif (not painted[(r0, c0)]) and (not painted[(r, c)]):
                if (r, c) not in black_cells:
                    break
                painted[(r0, c0)] = IS_RED
                painted[(r, c)] = IS_BLUE
                counter += 2
        else:
            return True
    else:
        return False
def main():
    h, w = map(int, input().split())
    black_cells = []
    for r in range(h):
        for c, cell in enumerate(input()):
            if cell == "#":
                black_cells.append((r, c))
    black_cells.sort()
    if solve(h, w, black_cells):
        print("YES")
    else:
        print("NO")
if __name__ == '__main__':
    main()
            
            
            
        
            
はむ吉🐹