四大文化赛道完整展开
03-execution/run-001/source-snapshot/main.py
main.py
站内文件视图直接读取仓库内容,Markdown 使用文档排版渲染,其余文本文件保持原始排版,方便校对训练证据链。
文件类型.py
10-cases/s1-jh-07-heritage-pattern-grid/03-execution/run-001/source-snapshot/main.py
from collections import deque
import sys
def solve(data: str) -> str:
raw_lines = [line.strip() for line in data.splitlines() if line.strip()]
if not raw_lines:
return ""
rows, cols = map(int, raw_lines[0].split())
grid = raw_lines[1:1 + rows]
visited = [[False] * cols for _ in range(rows)]
components = 0
best_area = 0
best_origin = (0, 0)
directions = ((1, 0), (-1, 0), (0, 1), (0, -1))
for row in range(rows):
for col in range(cols):
if grid[row][col] != "1" or visited[row][col]:
continue
components += 1
queue = deque([(row, col)])
visited[row][col] = True
area = 0
origin = (row + 1, col + 1)
while queue:
cur_row, cur_col = queue.popleft()
area += 1
cur_origin = (cur_row + 1, cur_col + 1)
if cur_origin < origin:
origin = cur_origin
for dr, dc in directions:
nxt_row = cur_row + dr
nxt_col = cur_col + dc
if not (0 <= nxt_row < rows and 0 <= nxt_col < cols):
continue
if visited[nxt_row][nxt_col] or grid[nxt_row][nxt_col] != "1":
continue
visited[nxt_row][nxt_col] = True
queue.append((nxt_row, nxt_col))
if area > best_area or (area == best_area and origin < best_origin):
best_area = area
best_origin = origin
return "\n".join(
[
f"components={components}",
f"largest={best_area}",
f"best_origin={best_origin[0]} {best_origin[1]}",
]
)
if __name__ == "__main__":
sys.stdout.write(solve(sys.stdin.read()).strip())
sys.stdout.write("\n")