結果

問題 No.179 塗り分け
ユーザー lam6er
提出日時 2025-03-31 17:58:43
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,805 bytes
コンパイル時間 143 ms
コンパイル使用メモリ 82,796 KB
実行使用メモリ 105,940 KB
最終ジャッジ日時 2025-03-31 17:59:34
合計ジャッジ時間 21,011 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 36 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

h, w = map(int, input().split())
grid = [input().strip() for _ in range(h)]
s = set()
for i in range(h):
    for j in range(w):
        if grid[i][j] == '#':
            s.add((i, j))

if len(s) < 2:
    print("NO")
    exit()

def solve():
    for dx in range(-h+1, h):
        for dy in range(-w+1, w):
            if dx == 0 and dy == 0:
                continue
            mandatory_r = set()
            for (x, y) in s:
                pred = (x - dx, y - dy)
                if pred not in s:
                    mandatory_r.add((x, y))
            # Check step b: shifted mandatory_r are in s
            b_mandatory = set()
            valid = True
            for (x, y) in mandatory_r:
                bx, by = x + dx, y + dy
                if (bx, by) not in s:
                    valid = False
                    break
                b_mandatory.add((bx, by))
            if not valid:
                continue
            # Check if mandatory_r and b_mandatory are disjoint
            if mandatory_r & b_mandatory:
                continue
            s_remaining = s - (mandatory_r | b_mandatory)
            # Check cycles in s_remaining
            visited = set()
            valid_cycles = True
            for cell in s_remaining:
                if cell in visited:
                    continue
                current = cell
                path = []
                while True:
                    if current not in s_remaining:
                        valid_cycles = False
                        break
                    if current in visited:
                        if current in path:
                            idx = path.index(current)
                            cycle = path[idx:]
                            if len(cycle) % 2 != 0:
                                valid_cycles = False
                        break
                    visited.add(current)
                    path.append(current)
                    next_x = current[0] + dx
                    next_y = current[1] + dy
                    current = (next_x, next_y)
                if not valid_cycles:
                    break
            if not valid_cycles:
                continue
            # Check both R and B are non-empty
            # Check if mandatory_r is non-empty
            r_possible = len(mandatory_r) > 0 or len(s_remaining) >= 2
            # b_mandatory can be non-empty or s_remaining's other half
            b_possible = len(b_mandatory) > 0 or len(s_remaining) >= 2
            if not (r_possible and b_possible):
                continue
            # Since even cycles are ensured, mandatory_r can be zero but s_remaining has even pairs
            if mandatory_r or len(s_remaining) > 0:
                print("YES")
                return
    print("NO")

solve()
0