結果

問題 No.348 カゴメカゴメ
ユーザー lam6er
提出日時 2025-04-16 16:30:34
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,215 bytes
コンパイル時間 229 ms
コンパイル使用メモリ 82,164 KB
実行使用メモリ 210,428 KB
最終ジャッジ日時 2025-04-16 16:32:08
合計ジャッジ時間 15,642 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21 WA * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

class CycleNode:
    def __init__(self, strength, parent=None):
        self.strength = strength
        self.parent = parent
        self.children = []

def main():
    n, m = map(int, sys.stdin.readline().split())
    grid = [sys.stdin.readline().strip() for _ in range(n)]
    
    visited_x = [[False for _ in range(m)] for _ in range(n)]
    visited_dot = [[-1 for _ in range(m)] for _ in range(n)]
    
    initial_region = []
    for i in range(n):
        for j in range(m):
            if (i == 0 or i == n-1 or j == 0 or j == m-1) and grid[i][j] == '.':
                initial_region.append((i, j))
    
    for i, j in initial_region:
        visited_dot[i][j] = 0
    
    regions = deque()
    regions.append((initial_region, None))
    cycles = []
    current_region_id = 0
    
    while regions:
        current_region, parent_cycle = regions.popleft()
        edge_x = set()
        for (i, j) in current_region:
            for di, dj in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                ni, nj = i + di, j + dj
                if 0 <= ni < n and 0 <= nj < m and grid[ni][nj] == 'x' and not visited_x[ni][nj]:
                    edge_x.add((ni, nj))
        
        for (i, j) in edge_x:
            if not visited_x[i][j]:
                queue = deque()
                queue.append((i, j))
                visited_x[i][j] = True
                current_component = [(i, j)]
                while queue:
                    x, y = queue.popleft()
                    for dx in [-1, 0, 1]:
                        for dy in [-1, 0, 1]:
                            if dx == 0 and dy == 0:
                                continue
                            nx, ny = x + dx, y + dy
                            if 0 <= nx < n and 0 <= ny < m and grid[nx][ny] == 'x' and not visited_x[nx][ny]:
                                visited_x[nx][ny] = True
                                current_component.append((nx, ny))
                                queue.append((nx, ny))
                
                strength = len(current_component)
                new_cycle = CycleNode(strength, parent_cycle)
                if parent_cycle:
                    parent_cycle.children.append(new_cycle)
                else:
                    cycles.append(new_cycle)
                
                found = False
                new_region = []
                for (x, y) in current_component:
                    for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                        nx, ny = x + dx, y + dy
                        if 0 <= nx < n and 0 <= ny < m and grid[nx][ny] == '.' and visited_dot[nx][ny] == -1:
                            queue_dot = deque()
                            queue_dot.append((nx, ny))
                            visited_dot[nx][ny] = current_region_id + 1
                            new_region.append((nx, ny))
                            while queue_dot:
                                cx, cy = queue_dot.popleft()
                                for ddx, ddy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                                    ncx, ncy = cx + ddx, cy + ddy
                                    if 0 <= ncx < n and 0 <= ncy < m and grid[ncx][ncy] == '.' and visited_dot[ncx][ncy] == -1:
                                        visited_dot[ncx][ncy] = current_region_id + 1
                                        new_region.append((ncx, ncy))
                                        queue_dot.append((ncx, ncy))
                            regions.append((new_region, new_cycle))
                            current_region_id += 1
                            found = True
                            break
                    if found:
                        break
    
    def dfs(node):
        if not node:
            return (0, 0)
        take = node.strength
        not_take = 0
        for child in node.children:
            ct, cnt = dfs(child)
            take += cnt
            not_take += max(ct, cnt)
        return (take, not_take)
    
    total = 0
    for root in cycles:
        t, nt = dfs(root)
        total += max(t, nt)
    
    print(total)

if __name__ == "__main__":
    main()
0